JavaScript Interview Questions & Answers
(For 3+ Years Experience)
1. What are JavaScript data types?
JavaScript has primitive types (String, Number, Boolean, Null, Undefined, Symbol, BigInt) and
non-primitive types (Object, Array, Function).
2. Explain the difference between 'undefined' and 'null'.
'undefined' means a variable has been declared but not assigned. 'null' is an assignment value
indicating no value.
3. What is hoisting in JavaScript?
Hoisting is the behavior where variable and function declarations are moved to the top of their
scope.
4. How does JavaScript handle type coercion?
JavaScript automatically converts data types when comparing or operating on values of different
types, e.g., '5' + 1 becomes '51'.
5. What are arrow functions and how do they differ from regular functions?
Arrow functions have a concise syntax and do not bind their own 'this', making them ideal for
callbacks and methods.
6. What are higher-order functions? Give an example.
Functions that take other functions as arguments or return them. Example:
const double = x => x * 2;
const map = (arr, fn) => arr.map(fn);
7. What is a closure?
A closure is a function that has access to its outer function scope even after the outer function has
returned.
8. What are destructuring and spread/rest operators in JavaScript?
Destructuring extracts values from arrays or objects. Spread/rest '...' expands or gathers elements.
9. Explain 'this' keyword in different contexts.
'this' refers to the object it belongs to. In arrow functions, it refers to the outer scope. In classes, it
refers to the instance.
10. What is a Promise? Give an example.
A Promise is an object that represents eventual completion/failure of an async operation.
Example:
let promise = new Promise((resolve, reject) => resolve('Done'));
11. What is async/await? Provide a code example.
Async/await is syntactic sugar for Promises, allowing cleaner asynchronous code.
Example:
async function fetchData() {
let res = await fetch(url);
let data = await res.json();
12. How does the event loop work?
The event loop checks the call stack and task queue and processes tasks in order, allowing
non-blocking asynchronous behavior.
13. How do you select and manipulate DOM elements in JavaScript?
Using methods like getElementById, querySelector, and modifying properties like innerHTML,
classList, style, etc.
14. What are event listeners and how are they added/removed?
Use addEventListener and removeEventListener to attach/detach events.
element.addEventListener('click', handler);
15. How does try/catch/finally work in JavaScript?
try lets you test code; catch handles errors; finally executes regardless of the result.
16. What tools do you use for debugging JavaScript code?
Chrome DevTools, console.log, debugger statement, linters (ESLint), and IDE breakpoints.
17. How would you optimize a JavaScript-heavy web page?
Use lazy loading, minimize reflows, debounce inputs, use async scripts, and reduce DOM depth.
18. Explain how to handle API errors in async functions.
Use try/catch blocks, check response.ok, and show fallback UI to the user.
19. Write a debounce function.
function debounce(fn, delay) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
};
20. Reverse a string in JavaScript.
function reverseString(str) {
return str.split('').reverse().join('');
21. Find the second largest number in an array.
function secondLargest(arr) {
let unique = [...new Set(arr)].sort((a, b) => b - a);
return unique[1];