1. What are JavaScript data types?
JavaScript has 6 primitive data types: string, number, boolean, undefined, null, and symbol. It also has non-primitive types like objects and arrays.
2. What is a closure in JavaScript?
A closure is a function that retains access to its lexical scope, even when the function is executed outside that scope.
3. What is the difference between var, let, and const?
var has function scope and is hoisted.
let has block scope and is not hoisted in the same way.
const is similar to let, but its value cannot be reassigned.
4. What is the output of the following code? console.log([] + []);
The output is an empty string: "". When two arrays are added together, they are implicitly converted to strings and concatenated.
5. What is the difference between == and === in JavaScript?
== compares values after converting them to a common type (type coercion), while === compares both value and type (strict equality).
6. What are JavaScript callback functions?
A callback function is a function passed into another function as an argument to be executed later, typically after an asynchronous operation.
7. What is the output of the following code? console.log(1 + '1');
The output is "11", because JavaScript converts the number to a string and concatenates.
8. What is the purpose of the this keyword in JavaScript?
The this keyword refers to the context or object in which a function is called. It changes its value depending on how the function is invoked.
9. What is event delegation in JavaScript?
Event delegation is a technique where you attach a single event listener to a parent element instead of multiple listeners to child elements. This improves performance and simplifies handling dynamic elements.
10. What is the output of the following code? console.log(2 & 1);
The output is 0. This is the result of performing a bitwise AND operation between 2 and 1.
11. What is a JavaScript promise?
A JavaScript promise is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value.
12. What is the difference between a function declaration and a function expression?
Function declaration is hoisted and can be called before it appears in the code.
Function expression is not hoisted and can only be called after it is defined.
13. What is the output of the following code? console.log(typeof null);
The output is "object". This is a known bug in JavaScript, where the type of null is mistakenly returned as object.
14. How does the setTimeout function work in JavaScript?
The setTimeout function executes a function after a specified delay (in milliseconds). It only runs once after the delay.
15. What is a JavaScript closure example?
function outer() {
  let counter = 0;
  function inner() {
    counter++;
    return counter;
  }
  return inner;
}
const increment = outer();
console.log(increment()); // 1

The function increment is a closure that retains access to the counter variable defined in the outer function.

16. What is hoisting in JavaScript?
Hoisting is JavaScript's default behavior of moving declarations (but not initializations) to the top of the code during execution.
17. What is the difference between an array and an object in JavaScript?
An array is an ordered list of values, while an object is a collection of key-value pairs. Arrays have numeric indices, and objects use strings as keys.
18. What is the difference between apply() and call() methods?
Both apply() and call() are used to invoke a function with a specific this context. The difference is that apply() takes an array of arguments, while call() takes individual arguments.
19. What is the output of the following code? console.log([] == []);
The output is false. In JavaScript, objects (including arrays) are compared by reference, not by value.
20. What is the event loop in JavaScript?
The event loop is the mechanism that handles asynchronous operations in JavaScript. It allows non-blocking code execution and handles tasks like I/O, timers, and user interactions.
21. What is the difference between `undefined` and `null` in JavaScript?
`undefined` means a variable has been declared but has not yet been assigned a value. `null` is an assignment value that represents "no value" or "empty."
22. What is the output of the following code? console.log('5' - 3);
The output is 2, because JavaScript performs type coercion and converts the string to a number before performing the subtraction.
23. What are the different types of loops in JavaScript?
The common loops in JavaScript are `for`, `while`, `do...while`, and `for...in` (for iterating over object properties), `for...of` (for iterating over iterable objects like arrays).
24. What is the output of the following code? console.log(1 + '1');
The output is '11'. The `+` operator triggers string concatenation when one operand is a string.
25. What is a JavaScript module?
A module in JavaScript is a file that contains reusable code, and it can export functions, objects, or values for use in other files. Modules are used to maintain code modularity and improve maintainability.
26. What is the purpose of `async` and `await` in JavaScript?
`async` and `await` are used to handle asynchronous code in a more readable manner. `async` defines a function that returns a promise, and `await` pauses execution until the promise resolves.
27. What is the difference between `setTimeout()` and `setInterval()`?
`setTimeout()` runs a function once after a specified delay, while `setInterval()` executes a function repeatedly at regular intervals.
28. What is a `Map` in JavaScript?
A `Map` is a collection of key-value pairs where both keys and values can be any type (objects, functions, etc.), unlike objects where keys are always strings or symbols.
29. What is the output of the following code? console.log([] + []);
The output is an empty string: "", because adding two empty arrays results in an empty string due to type coercion.
30. What is the difference between `map()` and `forEach()` in JavaScript?
map() creates a new array with the results of calling a function on every element of the array.
forEach() executes a provided function once for each array element but does not return a new array.
31. How do you create a deep copy of an object in JavaScript?
One way to create a deep copy of an object is by using JSON.parse(JSON.stringify(obj)), though this method doesn't copy functions or special objects like Date.
32. What is the output of the following code? console.log(0 == false);
The output is true, because 0 and false are considered equal in JavaScript due to type coercion.
33. What is an IIFE (Immediately Invoked Function Expression)?
An IIFE is a function that is defined and executed immediately after its declaration. It is commonly used to create a private scope to avoid polluting the global namespace.
Example: (function() {
  console.log("I am an IIFE!");
})();
34. What is the purpose of the `bind()` method in JavaScript?
The `bind()` method creates a new function that, when called, has its `this` keyword set to the provided value, and prepends any given arguments to the function's arguments.
35. What is a `Promise` chain?
A promise chain is a sequence of promises where each promise is executed in sequence, allowing you to handle asynchronous tasks step by step. Each `.then()` returns a new promise.
36. What is the difference between `slice()` and `splice()` in JavaScript?
slice() returns a shallow copy of a portion of an array without modifying the original array.
splice() changes the contents of an array by removing, replacing, or adding elements.
37. What are arrow functions in JavaScript?
Arrow functions are a shorthand syntax for writing functions. They are anonymous and do not have their own `this` context.
Example: const add = (a, b) => a + b;
38. What is the output of the following code? console.log('10' - 1);
The output is 9, because JavaScript performs type coercion and converts the string to a number before performing the subtraction.
39. What is a `Set` in JavaScript?
A `Set` is a collection of unique values, meaning no duplicates are allowed. Sets are iterable and can store any type of value (primitives or objects).
40. What is the output of the following code? console.log('1' == 1);
The output is true, because JavaScript performs type coercion and compares the values of the operands after converting them to the same type.
41. What is event bubbling in JavaScript?
Event bubbling is a process where an event starts from the target element and bubbles up to the root of the DOM tree, triggering all ancestor event listeners.
42. How can you prevent event bubbling in JavaScript?
You can prevent event bubbling using event.stopPropagation() to stop the event from propagating to the parent elements.
43. What is the difference between `for...in` and `for...of` in JavaScript?
for...in iterates over the keys of an object or array.
for...of iterates over the values of an iterable object like an array.
44. What is the purpose of `localStorage` and `sessionStorage` in JavaScript?
`localStorage` stores data persistently across page reloads and browser sessions, while `sessionStorage` stores data for the duration of a page session (until the browser tab is closed).
45. What is the output of the following code? console.log([1] == [1]);
The output is false, because arrays are compared by reference, not by value, and thus they are not the same object in memory.
46. What are higher-order functions in JavaScript?
Higher-order functions are functions that either take one or more functions as arguments, return a function, or both.
47. What is the `reduce()` function in JavaScript?
The `reduce()` function is used to accumulate values from an array into a single result. It iterates over the array and applies a function to accumulate the result.
Example: arr.reduce((acc, curr) => acc + curr, 0);
48. What is the difference between `slice()` and `splice()` in JavaScript?
slice() returns a shallow copy of a portion of an array.
splice() changes the original array by adding/removing elements.
49. What is the difference between an `instanceof` and `typeof` in JavaScript?
`instanceof` checks if an object is an instance of a particular class or constructor function.
`typeof` returns the type of a variable, e.g., "string", "number", "object".
50. What is the purpose of `setImmediate()` in JavaScript?
`setImmediate()` schedules a callback to be executed immediately after the current event loop cycle, but after I/O events.
51. What is the difference between `==` and `===` in JavaScript?
`==` compares values with type coercion, while `===` compares both values and types, meaning it checks for strict equality.
52. What is the output of the following code? console.log([] == ![]);
The output is true, because `[]` is falsy and `![]` is also falsy. Thus, `==` compares both to be equal.
53. What is `closure` in JavaScript?
A closure is a function that "remembers" its lexical scope, even when the function is executed outside of that scope.
54. What is the `this` keyword in JavaScript?
`this` refers to the context in which a function is called, pointing to the object that called the function (or `undefined` in strict mode).
55. What is the event loop in JavaScript?
The event loop is the mechanism that handles the execution of multiple code statements in an asynchronous, non-blocking manner in JavaScript.
56. What is the output of the following code? console.log('0' == []);
The output is true, because JavaScript converts both the string `'0'` and the array `[]` to falsy values during comparison.
57. What is a `prototype` in JavaScript?
A `prototype` is an object that is associated with every JavaScript function and object, allowing inheritance of properties and methods.
58. What is the purpose of the `call()` method in JavaScript?
The `call()` method is used to invoke a function with a specific `this` value and arguments provided individually.
59. What is the `apply()` method in JavaScript?
The `apply()` method is similar to `call()`, but the arguments are passed as an array or array-like object.
60. What is a `debounce` function in JavaScript?
A debounce function ensures that a function is not executed too frequently by delaying its execution until a specified amount of time has passed since its last call.
61. What is a `throttle` function in JavaScript?
A throttle function limits the execution of a function to once every specified amount of time, ensuring it doesn’t execute too frequently.
62. What is the `new` keyword in JavaScript?
The `new` keyword is used to create an instance of a constructor function or a class. It creates a new object, sets the constructor's `this`, and runs the constructor function.
63. What is the output of the following code? console.log(typeof null);
The output is object, which is a quirk of JavaScript's type system.
64. What is a `setTimeout` callback function in JavaScript?
The `setTimeout` function accepts a callback function to be executed after a specified delay. It’s often used to introduce delay or delay the execution of a function.
65. What is the `typeof` operator in JavaScript?
The `typeof` operator is used to determine the type of a given operand. It returns a string representing the type (e.g., "number", "string", "object").
66. What is the `instanceof` operator in JavaScript?
The `instanceof` operator checks if an object is an instance of a specific class or constructor function.
67. What is `setInterval()` in JavaScript?
`setInterval()` is used to repeatedly execute a function at a specified interval in milliseconds until cleared.
68. What is the output of the following code? console.log([1] + [1]);
The output is '1,1', because JavaScript converts both arrays into strings and joins them with a comma.
69. How can you add an element to the beginning of an array in JavaScript?
You can use the `unshift()` method to add an element to the beginning of an array.
70. How can you add an element to the end of an array in JavaScript?
You can use the `push()` method to add an element to the end of an array.
71. What is a `getter` and `setter` in JavaScript?
Getters and setters are methods used to access or set the value of an object's properties. They allow you to control access to object properties.
Example: class Person {
  get name() { return this._name; }
  set name(value) { this._name = value; }
}
72. What is the `delete` operator in JavaScript?
The `delete` operator is used to remove a property from an object or an element from an array.
73. What is a `weakMap` in JavaScript?
A `WeakMap` is similar to a `Map`, but the keys are weakly held. This means that if the key object is garbage-collected, the entry will be removed from the map automatically.
74. What is `strict mode` in JavaScript?
Strict mode is a way to opt into a restricted version of JavaScript. It prevents the use of certain dangerous language features and makes debugging easier by throwing errors for unsafe operations.
75. What is the output of the following code? console.log([1, 2, 3] == [1, 2, 3]);
The output is false, because arrays are reference types, and the references are not the same.
76. What is a `symbol` in JavaScript?
A `symbol` is a unique and immutable primitive value used to create object property keys that are guaranteed to be unique.
77. What is the `navigator` object in JavaScript?
The `navigator` object provides information about the user's browser, operating system, and other environment details.
78. How can you check if a value is an array in JavaScript?
You can use the `Array.isArray()` method to check if a value is an array.
79. What is the difference between `var`, `let`, and `const`?
`var` is function-scoped and can be re-declared, while `let` and `const` are block-scoped. `const` cannot be reassigned, whereas `let` can.
80. What is the output of the following code? console.log(1 + '1');
The output is '11', because JavaScript performs string concatenation when one operand is a string.
81. What is the `globalThis` object in JavaScript?
The `globalThis` object provides a standard way to access the global object across different environments (browser, Node.js, etc.).
82. What is an event delegation in JavaScript?
Event delegation is a technique in which you attach a single event listener to a parent element and handle events for child elements via event propagation.
83. How do you handle errors in JavaScript?
Errors in JavaScript can be handled using `try...catch` blocks, where the code that may throw an error is placed inside the `try` block, and the error is caught in the `catch` block.
84. What is the `fetch()` API in JavaScript?
The `fetch()` API is used to make asynchronous requests to retrieve or send data from/to a server. It returns a promise that resolves to the response object.
85. How can you check if a property exists in an object in JavaScript?
You can use the `in` operator or the `hasOwnProperty()` method to check if a property exists in an object.
86. What is `super()` in JavaScript?
`super()` is used to call the constructor of a parent class from a child class or to call methods from a parent class.
87. What is destructuring in JavaScript?
Destructuring allows you to unpack values from arrays or properties from objects into distinct variables.
88. What is the `concat()` method in JavaScript?
The `concat()` method is used to join two or more arrays into a new array.
89. What is the `filter()` method in JavaScript?
The `filter()` method creates a new array with all elements that pass a given test function.
90. What is the `find()` method in JavaScript?
The `find()` method returns the first element in an array that satisfies the provided testing function.
91. What is the `flat()` method in JavaScript?
The `flat()` method is used to flatten an array of arrays into a single array.
92. What is the `reduceRight()` method in JavaScript?
The `reduceRight()` method applies a function against an accumulator and each element in an array (from right to left) to reduce it to a single value.
93. What is the `every()` method in JavaScript?
The `every()` method checks if all elements in an array pass the test implemented by the provided function.
94. What is the `some()` method in JavaScript?
The `some()` method checks if at least one element in an array passes the test implemented by the provided function.
95. What is the `join()` method in JavaScript?
The `join()` method is used to join all elements of an array into a single string, separated by a specified delimiter.
96. How can you check if a variable is NaN in JavaScript?
You can use the `isNaN()` function to check if a variable is `NaN` (Not a Number).
97. What is `window` in JavaScript?
`window` is the global object in a browser environment. It represents the browser's window or frame.
98. How can you create a deep copy of an array in JavaScript?
You can use the `JSON.parse(JSON.stringify(arr))` method to create a deep copy of an array, although this does not copy methods or special objects like `Date`.
99. How can you find the index of an element in an array in JavaScript?
You can use the `indexOf()` method to find the index of an element in an array.
100. How do you create an object in JavaScript?
You can create an object using object literal notation: const obj = { key: 'value' } or using the `new Object()` constructor.