Here are Top 50 JavaScript Interview Questions and Answers, ideal for beginner to intermediate candidates preparing for front-end or full-stack roles:
🔹 Basic JavaScript Questions
1. What is JavaScript?
JavaScript is a lightweight, interpreted programming language used to make web pages interactive.
2. Difference between var
, let
, and const
?
var
: function-scoped, can be re-declared and updated.let
: block-scoped, can be updated, not re-declared in the same scope.const
: block-scoped, cannot be updated or re-declared.
3. What are data types in JavaScript?
- Primitive: Number, String, Boolean, Null, Undefined, Symbol, BigInt
- Non-Primitive: Object, Array, Function
4. What is undefined
vs null
?
undefined
: variable declared but not assigned.null
: explicitly assigned to represent “no value”.
5. What are template literals?
ES6 feature using backticks (`) allowing embedded expressions:
let name = "John";
console.log(`Hello, ${name}`);
🔹 Operators and Control Flow
6. Difference between ==
and ===
?
==
: loose equality (performs type coercion)===
: strict equality (no coercion)
7. What is the ternary operator?
Shorthand for if-else:
condition ? expr1 : expr2;
8. What are truthy and falsy values?
Falsy: false
, 0
, ""
, null
, undefined
, NaN
Everything else is truthy.
9. What is a switch statement?
Control structure for multiple conditions:
switch(value) {
case 1: break;
default: break;
}
🔹 Functions and Scope
10. What is a function in JavaScript?
Block of code designed to perform a task:
function greet() { return "Hello"; }
11. What are arrow functions?
ES6 syntax:
const add = (a, b) => a + b;
12. What is closure?
A function that retains access to its lexical scope even when executed outside that scope.
13. What is hoisting?
JavaScript moves declarations to the top of the scope.
14. What is the difference between function declaration and expression?
// Declaration
function foo() {}
// Expression
const foo = function() {};
🔹 Objects and Arrays
15. How to create an object?
let person = { name: "John", age: 30 };
16. How to access object properties?
Dot or bracket notation:
person.name // or person["name"]
17. How to loop through an object?
for (let key in object) { }
18. What are array methods like map
, filter
, reduce
?
map
: returns new array after applying function.filter
: filters elements based on condition.reduce
: reduces array to a single value.
🔹 DOM & Events
19. What is the DOM?
Document Object Model — structure of a webpage as a tree of objects.
20. How to select elements in the DOM?
document.getElementById('id');
document.querySelector('.class');
21. How to add an event listener?
element.addEventListener('click', function);
22. What is event bubbling vs capturing?
- Bubbling: event propagates from child to parent.
- Capturing: propagates from parent to child.
🔹 Asynchronous JavaScript
23. What is the event loop?
A mechanism to handle asynchronous code via the call stack and task queue.
24. What is a callback function?
A function passed as an argument to another function.
25. What are Promises?
Used for async operations:
new Promise((resolve, reject) => {});
26. What is async/await
?
Syntactic sugar for promises to write async code like synchronous.
🔹 ES6+ Features
27. What are default parameters?
Function parameters with default values:
function greet(name = "Guest") {}
28. What is destructuring?
Unpacking values:
const {name, age} = person;
29. What are spread and rest operators?
- Spread
...
to expand - Rest
...
to collect:
function sum(...args) {}
30. What are modules?
ES6 feature to export/import code:
export function greet() {}
import { greet } from './file.js';
🔹 Error Handling
31. How to handle errors?
Using try...catch
block.
32. What is throw
in JavaScript?
Used to create custom errors:
throw new Error("Something went wrong");
🔹 Advanced Concepts
33. What is the this
keyword?
Refers to the context object; its value depends on how a function is called.
34. What is the prototype chain?
A chain of objects used to implement inheritance.
35. What is the difference between call
, apply
, and bind
?
call
: invokes with argumentsapply
: invokes with arraybind
: returns new function
36. What is setTimeout
and setInterval
?
setTimeout
: delays execution.setInterval
: runs repeatedly after delay.
🔹 Browser and Storage
37. What is localStorage vs sessionStorage?
localStorage
: persists data with no expiration.sessionStorage
: data expires on session end.
38. How to detect browser type in JavaScript?
navigator.userAgent;
🔹 Miscellaneous
39. Difference between synchronous and asynchronous?
- Synchronous: blocks code execution.
- Asynchronous: non-blocking.
40. What is JSON?
JavaScript Object Notation — a format for storing and transporting data.
41. How to parse and stringify JSON?
JSON.parse(); JSON.stringify();
42. What is NaN?
“Not a Number” — result of invalid number operation.
43. What is the use of typeof
?
Returns the data type of a variable.
44. What are Immediately Invoked Function Expressions (IIFE)?
Function that runs as soon as it’s defined:
(function() {})();
45. What is memoization?
Caching function results to improve performance.
🔹 New/Modern Features
46. What is optional chaining (?.
)?
Safely access deeply nested properties:
obj?.a?.b;
47. What is nullish coalescing (??
)?
Provides fallback only for null
or undefined
:
let name = userName ?? "Guest";
48. What is the difference between Array vs Object?
- Array: ordered collection with numeric indices.
- Object: unordered collection with key-value pairs.
49. What is the debounce function?
Delays function execution until after a certain time has passed since the last call.
50. What are generators in JavaScript?
Functions that can be paused and resumed:
function* gen() {
yield 1;
yield 2;
}