Skip to main content

Complete Guide to JavaScript Interview Questions and Answers (2025)

 🔹 Basic JavaScript Questions



  1. What is JavaScript?
    JavaScript is a programming language used to make websites interactive (like buttons, forms, animations).

  2. How is JavaScript different from Java?
    Java is a full programming language (used for apps, backend, etc.). JavaScript mainly runs in browsers for web pages.

  3. What are the data types in JavaScript?
    Common
    ones: StringNumberBooleanObjectArraynullundefinedSymbolBigInt.

  4. Difference between varlet, and const?

    • var: old way, not block-scoped.

    • let: new, block-scoped.

    • const: same as let but cannot be changed.

  5. What is hoisting?
    JavaScript moves variable and function declarations to the top before running code.

  6. Difference between == and ===?

    • == compares value only (allows type conversion).

    • === compares value and type (strict).

  7. What are truthy and falsy values?

    • Truthy: values that act as true (like "hello"1).

    • Falsy: values that act as false (like 0""nullundefinedNaNfalse).

  8. What is typeof?
    It shows the data type of a value, e.g., typeof 5 returns "number".

  9. How do you declare a function?

    js
    function greet() { console.log("Hello"); }
  10. What are arrow functions?
    A shorter way to write functions:

js
const greet = () => console.log("Hi");

🔹 Scope & Closures

  1. Function vs block scope?

  • var is function-scoped.

  • let and const are block-scoped (work only inside {}).

  1. What is a closure?
    A function that remembers variables from its outer function, even after the outer function finishes.

  2. Why are closures useful?
    They help in keeping data private or building functions that remember state.

  3. What is lexical scope?
    Inner functions can use variables from outer functions (where they were defined, not called).


🔹 Functions and Objects

  1. What are higher-order functions?
    Functions that take other functions as arguments or return a function.

  2. Function declaration vs expression?

  • Declaration: function sum() {}

  • Expression: const sum = function() {}

  1. What is callapply, and bind?
    They let you run a function with a custom this value.

  • call: pass arguments normally

  • apply: pass arguments in array

  • bind: returns a new function with fixed this.

  1. What is this in JavaScript?
    Refers to the object that is calling the function.

  2. How does this behave in arrow functions?
    Arrow functions don’t have their own this, they use the this of the outer function.

  3. What is object destructuring?
    Breaking an object into variables:

js
const user = { name: "Ali", age: 25 }; const { name, age } = user;

🔹 Arrays and Strings

  1. Common array methods?

  • map: change each item

  • filter: pick some items

  • reduce: combine into one

  • push/popshift/unshift: add/remove

  1. How to remove duplicates from array?

js
[...new Set([1, 2, 2, 3])] // [1, 2, 3]
  1. How to sort an array of numbers?

js
arr.sort((a, b) => a - b);
  1. How to reverse a string?

js
"hello".split("").reverse().join(""); // "olleh"
  1. Difference between slice and splice?

  • slice: copies part of array.

  • splice: removes/changes original array.


🔹 Asynchronous JavaScript

  1. What is a callback?
    A function passed to another function to run later.

  2. What are Promises?
    A way to handle async code (something that will finish later).

  3. What is async/await?
    A cleaner way to write Promises.

  4. What is the event loop?
    It helps JavaScript handle many tasks (like clicks, timers) one by one.

  5. Microtasks vs Macrotasks?
    Microtasks (Promises) run before macrotasks (like setTimeout).


🔹 DOM Manipulation & Events

  1. What is the DOM?
    It’s how JavaScript sees and controls a webpage.

  2. How to select elements?
    document.getElementByIdquerySelector, etc.

  3. How to handle events?

js
element.addEventListener("click", function() {});
  1. Event bubbling vs capturing?

  • Bubbling: event moves from inner to outer.

  • Capturing: outer to inner.

  1. What is event delegation?
    Attach one event listener to a parent to handle child events.


🔹 Error Handling

  1. How to handle errors?
    Use try...catch.

  2. What is try...catch?
    A block that runs code and catches any errors.

  3. Difference between throw and try-catch?
    throw creates an error, try-catch catches and handles it.


🔹 ES6+ Features

  1. What is a template literal?
    A way to include variables in strings:

js
`Hello, ${name}`
  1. Default parameters?
    Set default value if none is passed:

js
function greet(name = "Guest") {}
  1. Rest and spread?

  • Rest ...args: collect values into an array.

  • Spread ...arr: split array into values.

  1. for...of vs for...in?

  • for...of: for arrays

  • for...in: for objects

  1. What are classes?
    A way to create objects using a blueprint.


🔹 Advanced Concepts

  1. What is a prototype?
    A hidden object from which other objects inherit.

  2. What is prototypal inheritance?
    Objects can share properties using prototypes.

  3. Difference between null and undefined?

  • null: empty on purpose

  • undefined: not assigned yet

  1. What is a memory leak?
    When your code keeps using memory but doesn’t free it.

  2. How does garbage collection work?
    JavaScript removes unused data automatically.

  3. What are modules?
    Code files you can import/export to organize JavaScript better.

  4. What is debouncing/throttling?
    Techniques to control how often a function runs (like on scroll or keypress).