Q1. What Is JavaScript?
ANS: JavaScript is a high-level, interpreted programming language that makes it possible to create interactive web pages and online apps with dynamic functionality. Commonly referred to as the universal language, Javascript is primarily used by developers for front-end and back-end work.
Q2. What Are the Different Data Types in JavaScript?
ANS: JavaScript has six primitive data types:
Number
String
Boolean
Null
Undefined
Symbol
It also has two compound data types:
Object
Array
Q3. What Is Hoisting in JavaScript?
ANS: Hoisting is a JavaScript concept that refers to the process of moving declarations to the top of their scope. This means that variables and functions can be used before they are declared, as long as they are declared before they are used in a function.
Q4. What is NaN property in JavaScript?
ANS: NaN property represents the “Not-a-Number” value. It indicates a value that is not a legal number. type of NaN will return a Number. To check if a value is NaN, we use the isNaN() function.
Q5. Is JavaScript a statically typed or a dynamically typed language?
ANS: JavaScript is a dynamically typed language. In a dynamically typed language, the type of a variable is checked during run-time in contrast to a statically typed language, where the type of a variable is checked during compile-time. Since JavaScript is a loosely(dynamically) typed language, variables in JS are not associated with any type. A variable can hold the value of any data type.
Q6. What Is the Difference Between null and undefined?
ANS: null is an assignment value that represents no value or an empty value,
Ex. Var x= null;
Console.log(x); // OUTPUT: null
while undefined is a variable that has been declared but not assigned a value.
Ex. Var (y);
Console.log(y) ; // OUTPUT: undefined
Q7. What Is the Purpose of the “this” Keyword in JavaScript?
ANS: This keyword refers to the object that is executing the current function or method. It allows access to object properties and methods within the context of that object.
Q8. What Is the Difference Between == and === Operators in JavaScript?
ANS: The equality == operator is a comparison operator that compares two values and returns true if they are equal.
The strict equality === operator is also a comparison operator, but it compares two values and returns true only if they are equal and of the same type.
Q9. What Is the Difference Between “var” and “let” Keywords in JavaScript?
ANS: The var and let keywords are both used to declare variables in JavaScript. However, there are some key differences between the two keywords.
The var keyword declares a global variable, which means that the variable can be accessed from anywhere in the code.
The let keyword declares a local variable, which means that the variable can only be accessed within the block of code where it is declared.
Q10. What Is Implicit Type Conversion in JavaScript?
ANS: Implicit type conversion is a JavaScript concept that refers to the automatic conversion of a value from one type to another. In JavaScript, this conversion follows a priority order that typically begins with strings, then numbers, and finally booleans. If you try to add a string to a number, JavaScript will implicitly convert the number to a string before performing the addition operation because strings have the highest priority in type conversion.
Q11. What Are Higher-Order Functions(HOF) in JavaScript?
ANS: Higher-order functions are functions that can accept other functions as arguments or return functions as their results. They enable powerful functional programming patterns in JavaScript.
Q12. What Is the Use of a Constructor Function in JavaScript?
ANS: A constructor function is a special type of function that is used to create objects. Constructor functions are used to define the properties and methods of an object.
Ex. function Person(name,age) {
this.name = name;
this.age = age;
}
Q13. What is the purpose of setTimeout() and setInterval()?
ANS: setTimeout(): Executes a function once after a specified delay (in milliseconds).
Ex . setTimeout(function() {
console.log("Hello after 2 seconds");
}, 2000);
setInterval(): Repeatedly executes a function at specified intervals (in milliseconds).
Ex setInterval(function() {
console.log("Hello every 2 seconds");
}, 2000);
Q14. What Is the Difference Between querySelector and getElementById?
ANS: querySelector is a more versatile method that allows you to select elements using CSS-like selectors,
while getElementById specifically selects an element with the specified ID.
Q15. What are callbacks?
ANS: A callback is a function that will be executed after another function gets executed. In javascript, functions are treated as first-class citizens, they can be used as an argument of another function, can be returned by another function, and can be used as a property of an object.
Functions that are used as an argument to another function are called callback functions.
Browse our course links: Java Training in Pune
To Join our FREE DEMO Session: Click Here
Q16. What is the spread operator in JavaScript?
ANS: The spread operator (...) allows you to expand elements of an iterable (like an array or object) into individual elements. It is used to make shallow copies of objects or arrays or to combine them.
Q17. What is DOM?
ANS: DOM stands for Document Object Model. DOM is a programming interface for HTML and XML documents. When the browser tries to render an HTML document, it creates an object based on the HTML document called DOM. Using this DOM, we can manipulate or change various elements inside the HTML document.
Q18. What is the use of promises in javascript?
ANS: Promises are used to handle asynchronous operations in JavaScript. Before promises, callbacks were used to handle asynchronous operations. However, due to the limited functionality of callbacks, using multiple callbacks to handle asynchronous code can lead to unmanageable code.
Promise object has four states -
Pending - Initial state of promise. This state represents that the promise has neither been fulfilled nor been rejected, it is in the pending state.
Fulfilled - This state represents that the promise has been fulfilled, meaning the async operation is completed.
Rejected - This state represents that the promise has been rejected for some reason, meaning the async operation has failed.
Settled - This state represents that the promise has been either rejected or fulfilled.
A promise is created using the Promise constructor which takes in a callback function with two parameters, resolve and reject respectively.
Q19. What Is the Difference Between splice() and slice()?
ANS: splice() is used to modify an array by adding, removing, or replacing elements at a specific position.
slice() is used to create a new array that contains a portion of an existing array, specified by the starting and ending indices.
Q20. What Is the Purpose of the reduce() Function in JavaScript?
ANS: The reduce() function is used to reduce an array to a single value by applying a function to each element and accumulating the result.
Q21. How Can You Remove Duplicates From an Array in JavaScript?
ANS: One way to remove duplicates from an array is by using the Set object or by using the filter() method with the indexOf() method.
Q22. What Are the Different Events in JavaScript?
There are many different events in JavaScript, but some of the most common events include:
Click: The click event occurs when a user clicks on an HTML element.
Mouseover: The mouseover event occurs when a user's mouse pointer moves over an HTML element.
Keydown: The keydown event occurs when a user presses a key on the keyboard.
Keyup: The keyup event occurs when a user releases a key on the keyboard.
Change: The change event occurs when a user changes the value of an HTML input element.
Q23. What Is the Scope of a Variable in JavaScript?
ANS: The scope of a variable in JavaScript is the part of the code where the variable can be accessed. Variables declared with the var keyword have a local scope, which means that they can only be accessed within the block of code where they are declared. Variables declared with the let keyword have a block scope, which means that they can only be accessed within the block of code where they are declared and any nested blocks. Variables declared with the const keyword have a global scope, which means that they can be accessed from anywhere in the code.
Q24. What Are the Different Ways to Access an HTML Element in JavaScript?
ANS: There are three main ways to access an HTML element in JavaScript:
Using the getElementById() method: The getElementById() method takes a string as an argument and returns the HTML element with the specified ID.
Using the getElementsByTagName() method: The getElementsByTagName() method takes a string as an argument and returns an array of all the HTML elements with the specified tag name.
Using the querySelector() method: The querySelector() method takes a CSS selector as an argument and returns the first HTML element that matches the selector.
Q25. What Are the Different Ways to Create Objects in JavaScript?
ANS: There are multiple ways to create objects in JavaScript, including object literals, constructor functions, the Object.create() method, and the class syntax introduced in ECMAScript 2015 (ES6).
Q26. What Is the Purpose of the async and await keywords in JavaScript?
ANS: The async and await keywords are used for handling asynchronous operations in a more synchronous-like manner. The async keyword is used to define an asynchronous function, and the await keyword is used to pause the execution of an async function until a promise is fulfilled or rejected.
Q27. What is the purpose of setImmediate() in JavaScript?
ANS: setImmediate() is used to execute a callback function immediately after the current event loop cycle. It's typically used to defer the execution of code until the next cycle.
Q28. What Is Event Delegation in JavaScript?
ANS: Event delegation is a technique where you attach a single event listener to a parent element, and that event listener handles events occurring on its child elements. It helps optimize performance and reduce memory consumption.
Q29. What is the difference between apply(), call(), and bind()?
ANS: apply() and call(): Both are used to call a function with a specified value and arguments.
call(): Accepts a list of arguments.
apply(): Accepts an array of arguments.
bind(): Creates a new function that, when called, has its this value set to the provided value and prepends any given arguments.
Q30. What are arrow functions in JavaScript?
ANS: Arrow functions provide a shorter syntax for writing functions. They also differ from regular functions in terms of how this is bound. Arrow functions do not have their own; instead, they inherit this from the surrounding context.
Browse our course links: Java Training in Pune
To Join our FREE DEMO Session: Click Here
Get More Information