# Top 50 JavaScript Interview Questions

#### 1\. What are the different data types in JavaScript?

Primitive types: `String`, `Number`, `BigInt`, `Boolean`, `undefined`, `Symbol`, `null`. Non-primitive: `Object`, `Array`, `Function`.

---

#### 2\. What is the difference between `var`, `let`, and `const`?

* `var`: function-scoped, hoisted
    
* `let`: block-scoped, not hoisted
    
* `const`: block-scoped, cannot be reassigned
    

---

#### 3\. What is hoisting?

Hoisting is JavaScript's default behavior of moving declarations to the top. `var` is hoisted with `undefined`; `let` and `const` are hoisted but not initialized.

---

#### 4\. What is the difference between `==` and `===`?

* `==` compares values after type coercion.
    
* `===` compares values and types strictly.
    

---

#### 5\. What is a closure?

A closure is a function that remembers variables from its outer scope even after the outer function has returned.

---

#### 6\. What is the difference between `null` and `undefined`?

* `undefined`: variable declared but not assigned.
    
* `null`: assigned by the programmer to represent "no value".
    

---

#### 7\. What is the event loop in JavaScript?

It handles asynchronous callbacks. The call stack runs tasks, and the event loop pushes async tasks from the queue when the stack is clear.

---

#### 8\. What is a Promise?

A Promise is an object representing the eventual completion or failure of an async operation.

---

#### 9\. How does `async/await` work?

It simplifies working with Promises. `await` pauses execution until the Promise resolves or rejects.

---

#### 10\. What are arrow functions?

Concise function syntax: `const fn = () => {}`. They don't have their own `this` or `arguments`.

---

#### 11\. What is `this` in JavaScript?

`this` refers to the object that called the function. Its value depends on how the function is invoked.

---

#### 12\. What is a higher-order function?

A function that takes another function as an argument or returns a function.

---

#### 13\. What is currying?

Breaking down a function that takes multiple arguments into a series of functions that each take one argument.

---

#### 14\. What are the different ways to create an object?

* Object literals
    
* Constructor functions
    
* `Object.create()`
    
* ES6 classes
    

---

#### 15\. What is the difference between `slice()` and `splice()`?

* `slice(start, end)`: returns a shallow copy
    
* `splice(start, deleteCount, ...items)`: modifies original array
    

---

#### 16\. What is the difference between `call`, `apply`, and `bind`?

* `call`: calls function with arguments individually
    
* `apply`: calls function with arguments as an array
    
* `bind`: returns a new function with bound `this`
    

---

#### 17\. What is the prototype chain?

Objects in JavaScript have a hidden `[[Prototype]]` property pointing to another object. This forms a chain for inheritance.

---

#### 18\. What is the difference between shallow copy and deep copy?

* Shallow copy copies references to nested objects.
    
* Deep copy clones all levels.
    

---

#### 19\. What is the difference between `map`, `filter`, and `reduce`?

* `map()`: transforms array elements
    
* `filter()`: filters based on condition
    
* `reduce()`: reduces to a single value
    

---

#### 20\. What are template literals?

String literals allowing embedded expressions: `Hello, ${name}`

---

#### 21\. What is destructuring?

Extracting values from arrays or objects into variables.

---

#### 22\. What is the spread operator (`...`)?

Expands iterable values into individual elements.

---

#### 23\. What is the rest operator?

Collects remaining elements into an array.

---

#### 24\. What is the difference between synchronous and asynchronous code?

* Synchronous: executes line by line.
    
* Asynchronous: continues without waiting for completion.
    

---

#### 25\. What are callbacks?

Functions passed as arguments to other functions to run later.

---

#### 26\. What is `typeof`?

Operator used to check the data type of a variable.

---

#### 27\. What is `instanceof`?

Checks if an object is an instance of a specific constructor.

---

#### 28\. What is NaN?

NaN stands for Not-a-Number. It results from invalid math operations.

---

#### 29\. How do you check if a value is an array?

Use `Array.isArray(value)`.

---

#### 30\. What is event bubbling?

When an event starts from the deepest target element and bubbles up to ancestors.

---

#### 31\. How do you stop event propagation?

Use `event.stopPropagation()`.

---

#### 32\. What is debouncing?

Delays execution of a function until after a wait time since the last call.

---

#### 33\. What is throttling?

Limits function execution to once in a specified interval.

---

#### 34\. What are modules in JavaScript?

Code separated into reusable files using `import`/`export` (ES Modules) or `require`/`module.exports` (CommonJS).

---

#### 35\. What is the difference between `localStorage`, `sessionStorage`, and `cookies`?

* `localStorage`: no expiry
    
* `sessionStorage`: cleared on tab close
    
* `cookies`: sent with every request
    

---

#### 36\. What are default parameters?

You can set default values for function parameters.

---

#### 37\. What is optional chaining (`?.`)?

Safely access nested properties without throwing errors if a property is `null` or `undefined`.

---

#### 38\. What is nullish coalescing (`??`)?

Returns the right-hand operand only if the left is `null` or `undefined`.

---

#### 39\. What is a symbol?

A unique and immutable primitive value often used as object keys.

---

#### 40\. What are WeakMap and WeakSet?

Similar to Map/Set but keys are weakly referenced and not enumerable.

---

#### 41\. What is a generator function?

A function that can be paused and resumed using `function*` and `yield`.

---

#### 42\. How do you handle exceptions in JavaScript?

Use `try...catch` blocks to handle runtime errors.

---

#### 43\. What is `strict mode`?

Adds stricter parsing and error handling to JavaScript.

---

#### 44\. What is tail call optimization?

An optimization technique where the last function call doesn’t add a new stack frame.

---

#### 45\. What is memoization?

Caching the results of expensive function calls to avoid re-computation.

---

#### 46\. What is the difference between `eval()` and `Function()`?

Both execute strings as code. `eval()` has access to scope; `Function()` creates new scope.

---

#### 47\. What are IIFEs?

Immediately Invoked Function Expressions: `(function(){})()`

---

#### 48\. What is object destructuring with default values?

```js
const { a = 10, b = 20 } = obj;
```

---

#### 49\. What is the difference between `for...in` and `for...of`?

* `for...in`: iterates over keys
    
* `for...of`: iterates over values (iterables)
    

---

#### 50\. What is tail recursion?

A recursive function where the recursive call is the last operation. Optimized to avoid stack overflows.
