Skip to content

Latest commit

 

History

History
704 lines (471 loc) · 21 KB

File metadata and controls

704 lines (471 loc) · 21 KB

📘 JavaScript Interview Questions


📚 Table of Contents

🔹 Basic JavaScript

  1. What are the data types in JavaScript?
  2. What is the difference between var, let, and const?
  3. What are primitive and non-primitive data types?
  4. What is hoisting in JavaScript?
  5. What is the difference between == and ===?
  6. Explain scope in JavaScript.
  7. What is a closure?
  8. What is a callback function?
  9. What is the difference between null and undefined?
  10. What is NaN and how can you check for it?
  11. What is the difference between function declaration and function expression?
  12. What is an IIFE (Immediately Invoked Function Expression)?
  13. What is event bubbling and event capturing?
  14. What is the difference between synchronous and asynchronous code?
  15. What is the typeof operator and how does it work?
  16. What are template literals?
  17. What is the use of this keyword?
  18. What is the use of bind(), call(), and apply()?
  19. What are arrow functions and how are they different from regular functions?
  20. What are truthy and falsy values in JavaScript?

🔸 Intermediate JavaScript

  1. What is the event loop in JavaScript?
  2. Explain how setTimeout and setInterval work.
  3. What is the difference between shallow copy and deep copy?
  4. What are JavaScript Promises? How do they work?
  5. What is async/await and how is it different from Promises?
  6. What is destructuring in JavaScript?
  7. What are spread and rest operators?
  8. What is the difference between for, for..in, and for..of loops?
  9. What are map, filter, and reduce methods?
  10. How does prototypal inheritance work in JavaScript?
  11. What is the difference between Object and Map?
  12. How do you handle errors in JavaScript?
  13. What is the use of Object.freeze() and Object.seal()?
  14. What are higher-order functions?
  15. What is a pure function?
  16. What is currying in JavaScript?
  17. What is the difference between debounce and throttle?
  18. What are the different ways to clone an object?
  19. How does garbage collection work in JavaScript?
  20. What is the use of the arguments object?

🔸 Advanced JavaScript

  1. What is memory leak and how can you prevent it?
  2. Explain the concept of closures with an example.
  3. What is the difference between Object.create() and constructor functions?
  4. How is async/await internally implemented?
  5. What are generators in JavaScript?
  6. What is the difference between deep equality and shallow equality?
  7. What is the Temporal Dead Zone?
  8. What are WeakMap and WeakSet?
  9. What is tail call optimization?
  10. What is the module pattern in JavaScript?
  11. What is Service Worker in JavaScript?
  12. What is the difference between localStorage, sessionStorage, and cookies?
  13. How does JavaScript handle concurrency?
  14. What is the difference between a microtask and a macrotask?
  15. What is the purpose of Symbol in JavaScript?
  16. What are dynamic imports?
  17. What is the difference between eval() and Function() constructor?
  18. What is reflow and repaint in the browser?
  19. What is serialization and deserialization?
  20. How can you make a custom iterable object in JavaScript?

🔹 Basic JavaScript

1. What are the data types in JavaScript?

JavaScript has 7 primitive data types:

  • Number
  • String
  • Boolean
  • Undefined
  • Null
  • Symbol
  • BigInt
    Non-primitive: Object (includes arrays, functions, etc.)

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

  • var: Function/global scoped, can be redeclared/updated.
  • let: Block scoped, can be updated but not redeclared in same scope.
  • const: Block scoped, cannot be updated/redeclared.

3. What are primitive and non-primitive data types?

  • Primitive: Immutable - String, Number, Boolean, Undefined, Null, Symbol, BigInt.
  • Non-primitive: Mutable - Object, Array, Function.

4. What is hoisting in JavaScript?

JS moves declarations (not initializations) to the top of their scope during compilation.


5. What is the difference between == and ===?

  • ==: Loose comparison (type coercion).
  • ===: Strict comparison (no coercion, checks type + value).

6. Explain scope in JavaScript.

  • Global: Available anywhere.
  • Function/Local: Inside a function.
  • Block: Within {} using let/const.

7. What is a closure?

A function that retains access to its lexical scope even when executed outside of it.


8. What is a callback function?

A function passed to another function and executed after some operation completes.


9. What is the difference between null and undefined?

  • null: Intentional absence of value.
  • undefined: Variable declared but not assigned.

10. What is NaN and how can you check for it?

  • NaN: Not-a-Number.
  • Check: Number.isNaN(val) or val !== val.

11. What is the difference between function declaration and function expression?

  • Declaration: Hoisted, available before definition.
  • Expression: Not hoisted, defined at runtime.

12. What is an IIFE (Immediately Invoked Function Expression)?

A function that runs immediately after being defined:

(function () {
  console.log("IIFE");
})();

13. What is event bubbling and event capturing?

  • Bubbling: Event flows from child to parent (default).
  • Capturing: Event flows from parent to child (useCapture = true).

14. What is the difference between synchronous and asynchronous code?

  • Synchronous: Blocks further execution.
  • Asynchronous: Non-blocking, tasks run in parallel.

15. What is the typeof operator and how does it work?

Returns a string of the operand’s type:

typeof "hello"; // "string"

16. What are template literals?

Strings enclosed in backticks that allow expression interpolation:

`Hello, ${name}`;

17. What is the use of this keyword?

Refers to the context object of the current function/execution.


18. What is the use of bind(), call(), and apply()?

  • bind: Returns new function with bound this.
  • call: Executes function with specified this and args.
  • apply: Same as call, but args passed as array.

19. What are arrow functions and how are they different from regular functions?

  • Shorter syntax, no own this.
  • Suitable for callbacks and preserving context.

20. What are truthy and falsy values in JavaScript?

  • Truthy: "0", 1, [], {}, etc.
  • Falsy: 0, "", null, undefined, NaN, false.

🔸 Intermediate JavaScript

21. What is the event loop in JavaScript?

The event loop is a mechanism that handles asynchronous code execution. It monitors the call stack and task queue, and once the call stack is empty, it pushes queued callbacks (like from setTimeout, Promises, or events) onto the stack for execution. This ensures non-blocking behavior in JavaScript.


22. How do setTimeout and setInterval work?

  • setTimeout(fn, delay): Executes fn once after the specified delay.
  • setInterval(fn, delay): Repeatedly executes fn every delay milliseconds until stopped with clearInterval.
setTimeout(() => console.log("After 2s"), 2000);
setInterval(() => console.log("Every 2s"), 2000);

23. Difference between shallow copy and deep copy?

  • Shallow copy: Copies only the top-level values. Nested objects are still referenced.
  • Deep copy: Recursively copies all levels, creating independent nested structures.
let shallow = [...array];
let deep = JSON.parse(JSON.stringify(object));

24. What are JavaScript Promises?

A Promise represents a future value from an asynchronous operation. It has three states:

  • Pending
  • Fulfilled
  • Rejected

Handled via .then(), .catch(), and .finally().

new Promise((resolve, reject) => {
  // async task
}).then(...).catch(...);

25. What is async/await?

async/await is syntactic sugar over Promises. It makes asynchronous code look synchronous and improves readability.

async function getData() {
  const res = await fetch(url);
  const data = await res.json();
  return data;
}

26. What is destructuring?

Destructuring allows unpacking values from arrays or objects into variables.

const { name, age } = { name: "Alice", age: 25 };

27. What are spread and rest operators?

  • Spread (...): Expands elements of an array/object.
  • Rest (...): Gathers multiple elements into one.
let newArr = [...arr, 4];
function sum(...nums) {
  return nums.reduce((a, b) => a + b);
}

28. Difference between for, for...in, and for...of?

  • for: Traditional loop over indices.
  • for...in: Iterates over object keys.
  • for...of: Iterates over iterable values (arrays, strings).
for (let i = 0; i < arr.length; i++) {}
for (let key in obj) {
}
for (let value of arr) {
}

29. What are map, filter, and reduce?

  • map(): Transforms elements.
  • filter(): Selects elements.
  • reduce(): Combines elements.
nums.map((x) => x * 2);
nums.filter((x) => x > 0);
nums.reduce((a, b) => a + b, 0);

30. How does prototypal inheritance work?

JavaScript objects inherit from a prototype. If a property isn't found on an object, it's looked up in the prototype chain.


31. Difference between Object and Map?

  • Object: Keys must be strings or symbols.
  • Map: Keys can be any type and preserves insertion order.
let map = new Map();
map.set("key", "value");

32. How to handle errors in JavaScript?

  • try...catch: Catches runtime errors.
  • throw: Manually raise errors.
  • finally: Runs always.
try {
  throw new Error("Oops");
} catch (e) {
  console.error(e);
} finally {
  console.log("Done");
}

33. Use of Object.freeze() and Object.seal()?

  • Object.freeze(): Prevents all changes.
  • Object.seal(): Allows value change but not addition/removal of properties.

34. What are higher-order functions?

Functions that accept or return other functions.

function apply(a, b, fn) {
  return fn(a, b);
}

35. What is a pure function?

A function that:

  • Returns the same output for the same input.
  • Has no side effects.

36. What is currying in JavaScript?

Transforming a function with multiple arguments into a sequence of unary functions.

const add = (x) => (y) => x + y;
add(5)(3); // 8

37. What is the difference between debounce and throttle?

  • Debounce: Delays execution until inactivity.
  • Throttle: Limits execution to once per interval.

38. What are the different ways to clone an object?

  • Shallow: Object.assign({}, obj) or {...obj}
  • Deep: JSON.parse(JSON.stringify(obj))

39. How does garbage collection work in JavaScript?

JS automatically removes unreachable memory (no references left) via garbage collection.


40. What is the use of the arguments object?

Available inside functions, it holds all passed arguments.

function sum() {
  return [...arguments].reduce((a, b) => a + b);
}

💠 Advanced JavaScript

51. What is memory leak and how can you prevent it?

A memory leak occurs when memory that is no longer needed is not released. In JavaScript, it can happen if there are lingering references to objects. To prevent it:

  • Remove event listeners when they are no longer needed.
  • Avoid global variables.
  • Nullify references to unused objects or elements.

52. Explain the concept of closures with an example.

A closure occurs when a function retains access to variables from its lexical scope, even after the outer function has finished executing.

Example:

function outer() {
  let counter = 0;
  return function inner() {
    counter++;
    console.log(counter);
  };
}
const increment = outer();
increment(); // 1
increment(); // 2

In this case, inner() has access to counter even after outer() has finished executing.


53. What is the difference between Object.create() and constructor functions?

  • Object.create(): Creates a new object with a specified prototype.
  • Constructor functions: A function used to create objects with this keyword and initialize their properties.
// Object.create()
let obj1 = Object.create({ name: "John" });

// Constructor function
function Person(name) {
  this.name = name;
}
let person1 = new Person("John");

54. How is async/await internally implemented?

async/await is syntactic sugar over Promises. Internally, async functions return a Promise. When await is used, the function execution pauses until the Promise resolves or rejects, without blocking the main thread.


55. What are generators in JavaScript?

A generator is a function that can be paused and resumed, allowing for asynchronous-like behavior without callbacks or promises. It uses function* syntax and yield to pause execution.

Example:

function* count() {
  yield 1;
  yield 2;
  yield 3;
}
const gen = count();
console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }

56. What is the difference between deep equality and shallow equality?

  • Shallow equality compares object references, meaning if objects are nested, only the top-level properties are checked.
  • Deep equality compares both top-level and nested properties recursively to ensure the objects are identical.

57. What is the Temporal Dead Zone?

The Temporal Dead Zone (TDZ) is the period between entering the scope and the actual declaration of a variable. During this time, accessing the variable results in a ReferenceError.

Example:

console.log(a); // ReferenceError: Cannot access 'a' before initialization
let a = 5;

58. What are WeakMap and WeakSet?

  • WeakMap: A collection of key-value pairs where keys are objects, and values can be any type. It does not prevent garbage collection of keys when they are no longer referenced.
  • WeakSet: A collection of unique objects. Like WeakMap, it does not prevent garbage collection.

59. What is tail call optimization?

Tail call optimization (TCO) is an optimization technique where the last call in a function is optimized to avoid creating a new stack frame, allowing recursion without growing the call stack.

Example:

function factorial(n, acc = 1) {
  if (n === 0) return acc;
  return factorial(n - 1, n * acc); // Tail call
}

60. What is the module pattern in JavaScript?

The module pattern encapsulates functionality and creates private variables and methods, exposing only the necessary parts of the module.

Example:

const counterModule = (function () {
  let count = 0;
  return {
    increment: function () {
      count++;
    },
    getCount: function () {
      return count;
    },
  };
})();

61. What is Service Worker in JavaScript?

A Service Worker is a script that runs in the background, separate from the web page, and is used for caching assets, handling push notifications, and enabling offline functionality.


62. What is the difference between localStorage, sessionStorage, and cookies?

  • localStorage: Stores data with no expiration time. Data persists across sessions.
  • sessionStorage: Stores data for the duration of the page session. It is cleared when the page is closed.
  • Cookies: Stores data with a specific expiration date and can be sent with every HTTP request.

63. How does JavaScript handle concurrency?

JavaScript uses an event loop to handle concurrency. It runs tasks asynchronously by adding them to a queue and processes them when the call stack is empty.


64. What is the difference between a microtask and a macrotask?

  • Microtasks: Small tasks that are executed after the current script finishes but before rendering. Promises' .then(), .catch(), and .finally() are microtasks.
  • Macrotasks: Larger tasks like setTimeout() and setInterval(), which are executed after microtasks.

65. What is the purpose of Symbol in JavaScript?

A Symbol is a primitive data type used to create unique identifiers for object properties. It ensures that property names are unique, avoiding name collisions.

Example:

const id = Symbol("id");
let obj = { [id]: 123 };

66. What are dynamic imports?

Dynamic imports allow you to load modules at runtime instead of during the initial loading. This helps with lazy loading.

Example:

import("./myModule").then((module) => {
  module.default();
});

67. What is the difference between eval() and Function() constructor?

  • eval(): Executes JavaScript code represented as a string.
  • Function() constructor: Creates a new function from a string, but does not execute the code immediately.

68. What is reflow and repaint in the browser?

  • Reflow: The process of recalculating the layout of the webpage (e.g., resizing elements).
  • Repaint: The process of re-rendering elements without affecting the layout (e.g., changing color).

69. What is serialization and deserialization?

  • Serialization: The process of converting an object into a format (usually JSON) that can be easily stored or transmitted.
  • Deserialization: The process of converting the serialized format back into an object.

70. How can you make a custom iterable object in JavaScript?

To make an object iterable, you need to define the Symbol.iterator method. This method should return an iterator with next() method.

Example:

const myIterable = {
  data: [1, 2, 3],
  [Symbol.iterator]: function () {
    let index = 0;
    return {
      next: () => {
        if (index < this.data.length) {
          return { value: this.data[index++], done: false };
        }
        return { done: true };
      },
    };
  },
};