1. What is the purpose of the typeof operator in JavaScript?
Step-by-Step Explanation
The typeof operator returns a string indicating the data type of a value (e.g., "string", "number", "boolean", "object", "undefined").
Answer: b) To determine the data type of a value. ✅
2. What is the difference between == and === operators in JavaScript?
Step-by-Step Explanation
== performs type coercion before comparison, while === does not. === is also called "strict equality".
Answer: b) `==` compares only values, `===` compares values and types. ✅
3. What is a closure in JavaScript?
Step-by-Step Explanation
Closures allow inner functions to "remember" the environment in which they were created.
Answer: b) A function that has access to variables from its outer (enclosing) function's scope, even after the outer function has finished executing. ✅
4. What is the purpose of the addEventListener() method?
Step-by-Step Explanation
addEventListener() is used to attach event listeners to HTML elements, allowing you to respond to user interactions or other events.
Answer: b) To add an event handler to an element. ✅
5. What does the this keyword refer to in a regular function?
Step-by-Step Explanation
In regular functions, this refers to the global object unless the function is called as a method of an object.
Answer: a) The global object (window in browsers). ✅
6. What is the purpose of the map() method for arrays?
Step-by-Step Explanation
map() transforms each element of an array and returns a new array.
Answer: b) To iterate over each element and create a new array with the results of a provided function. ✅
7. What is the purpose of the JSON.stringify() method?
Step-by-Step Explanation
JSON.stringify() converts a JavaScript object or value to a JSON string.
Answer: b) To convert a JavaScript object into a JSON string. ✅
8. What is the purpose of the setTimeout() function?
Step-by-Step Explanation
setTimeout() schedules a function to be executed after a specified number of milliseconds.
Answer: b) To execute a function after a specified delay. ✅
9. What is the difference between let and var keywords for variable declaration?
Step-by-Step Explanation
let variables are limited in scope to the block, statement, or expression where they are used. var variables are scoped to the function within which they are declared.
Answer: b) `let` has block scope, `var` has function scope. ✅
10. What is the purpose of the filter() method for arrays?
Step-by-Step Explanation
filter() creates a new array with all elements that pass the test implemented by the provided function.
Answer: b) To create a new array with elements that pass a test implemented by a provided function. ✅
11. What is the purpose of the Promise object in JavaScript?
Step-by-Step Explanation
Promise objects represent the eventual completion (or failure) of an asynchronous operation and its resulting value.
Answer: b) To handle asynchronous operations and their eventual completion or failure. ✅
12. What is event bubbling in JavaScript?
Step-by-Step Explanation
Event bubbling is the default behavior where an event propagates from the target element up through its parent elements.
Answer: b) The process of an event propagating from the target element to its ancestor elements. ✅
13. What is the purpose of the document.querySelector() method?
Step-by-Step Explanation
document.querySelector() returns the first element within the document that matches the specified selector, or null if no matching elements are found.
Answer: b) To select the first element that matches a CSS selector. ✅
14. What is the purpose of the reduce() method for arrays?
Step-by-Step Explanation
reduce() applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
Answer: c) To execute a reducer function on each element of the array, resulting in a single output value. ✅
15. What is the purpose of the async and await keywords?
Step-by-Step Explanation
async and await are used to write asynchronous code that is easier to read and understand.
Answer: b) To simplify asynchronous programming by making it look like synchronous code. ✅
16. What is the purpose of the localStorage object?
Step-by-Step Explanation
localStorage stores data with no expiration time, and the data is not sent to the server with each request.
Answer: b) To store data persistently in the browser, even after the browser is closed. ✅
17. What is the purpose of the Array.isArray() method?
Step-by-Step Explanation
Array.isArray() determines whether the passed value is an Array.