JavaScript Interview Questions and Answers
Q1: What is the difference between '==' and '===' in JavaScript?
A1: '==' compares values after type coercion, whereas '===' compares both value and type without
coercion.
Q2: What are closures in JavaScript?
A2: A closure is a function that has access to its own scope, the outer function's scope, and the
global scope, even after the outer function has returned.
Q3: What is the event loop in JavaScript?
A3: The event loop handles asynchronous operations by placing them in a queue and executing
them after the main stack is clear.
Q4: What is hoisting in JavaScript?
A4: Hoisting is JavaScript's behavior of moving declarations to the top of the current scope before
code execution.
Q5: What are promises in JavaScript?
A5: Promises represent the eventual completion (or failure) of an asynchronous operation and its
resulting value.
Q6: What is the difference between var, let, and const?
A6: 'var' is function-scoped, 'let' and 'const' are block-scoped. 'const' cannot be reassigned.
Q7: What is a callback function?
A7: A callback is a function passed into another function as an argument to be executed later.
Q8: What is the difference between null and undefined?
A8: 'null' is an assigned value representing no value. 'undefined' means a variable has been
declared but not assigned a value.
Q9: What is the use of the 'this' keyword?
A9: 'this' refers to the object it belongs to, and its value depends on how the function is called.
Q10: What is a JavaScript object?
A10: An object is a collection of key-value pairs, where values can be any type.
Q11: What is a prototype in JavaScript?
A11: Every JavaScript object has a prototype, from which it can inherit properties and methods.
Q12: What is the difference between call(), apply(), and bind()?
A12: 'call()' and 'apply()' invoke a function with a given 'this' context. 'bind()' returns a new function
with bound 'this'.
Q13: What is event delegation?
A13: Event delegation is a technique of handling events at a higher level in the DOM to manage
events for multiple child elements.
Q14: What is the difference between synchronous and asynchronous code?
A14: Synchronous code is executed sequentially, while asynchronous code can be executed without
blocking subsequent code.
Q15: What is the purpose of the async/await keywords?
A15: 'async' declares a function that returns a promise. 'await' pauses execution until the promise is
resolved.
Q16: What are arrow functions?
A16: Arrow functions are a shorter syntax for writing functions, and they do not have their own 'this'.
Q17: What is destructuring in JavaScript?
A17: Destructuring allows unpacking values from arrays or properties from objects into distinct
variables.
Q18: What is the spread operator?
A18: The spread operator (...) allows an iterable to be expanded in places where zero or more
arguments or elements are expected.
Q19: What is the rest parameter?
A19: The rest parameter allows representing an indefinite number of arguments as an array.
Q20: What is a higher-order function?
A20: A function that takes another function as an argument or returns a function.
Q21: What are template literals?
A21: Template literals allow embedded expressions and multi-line strings using backticks (` `).
Q22: What is a promise chain?
A22: A promise chain is a series of promises chained together using .then().
Q23: What is the purpose of the try/catch block?
A23: try/catch is used to handle errors gracefully in JavaScript.
Q24: What is the difference between forEach and map?
A24: 'forEach' executes a function for each array element. 'map' returns a new array with results.
Q25: What is a pure function?
A25: A pure function returns the same result for the same input and does not cause side effects.
Q26: What is immutability?
A26: Immutability means that an object's state cannot be modified after it is created.
Q27: What is the difference between slice and splice?
A27: 'slice' returns a shallow copy of a portion of an array. 'splice' modifies the original array.
Q28: What is a shallow copy and deep copy?
A28: Shallow copy copies references to nested objects. Deep copy duplicates all levels.
Q29: What are falsy values in JavaScript?
A29: Falsy values include false, 0, '', null, undefined, and NaN.
Q30: What is NaN in JavaScript?
A30: NaN stands for 'Not-a-Number'. It's a value representing undefined or unrepresentable
mathematical operations.
Q31: How does JavaScript handle memory management?
A31: JavaScript uses automatic garbage collection to reclaim memory occupied by unreachable
objects.
Q32: What are modules in JavaScript?
A32: Modules allow splitting code into reusable pieces. ES6 introduced 'import' and 'export' syntax.
Q33: What is the difference between function declaration and expression?
A33: Function declarations are hoisted. Function expressions are not.
Q34: What is currying in JavaScript?
A34: Currying transforms a function with multiple arguments into a sequence of functions each
taking one argument.
Q35: What is a Symbol in JavaScript?
A35: Symbols are unique and immutable primitive values often used as object property keys.
Q36: What is optional chaining?
A36: Optional chaining (?.) allows reading nested object properties without checking each level
manually.
Q37: What is nullish coalescing?
A37: The nullish coalescing operator (??) returns the right-hand operand when the left is null or
undefined.
Q38: What is debounce in JavaScript?
A38: Debounce limits the rate at which a function is executed by delaying it until after a pause.
Q39: What is throttle in JavaScript?
A39: Throttle limits the execution of a function to once every specified time interval.
Q40: What are Web APIs?
A40: Web APIs are built-in browser APIs for tasks like DOM manipulation, HTTP requests, timers,
etc.
Q41: What is localStorage?
A41: localStorage allows storing key-value pairs in a web browser with no expiration.
Q42: What is sessionStorage?
A42: sessionStorage is similar to localStorage but is cleared when the session ends.
Q43: What is the difference between innerHTML and innerText?
A43: 'innerHTML' gets/sets HTML content, while 'innerText' deals with visible text content only.
Q44: What is the DOM?
A44: DOM (Document Object Model) is a programming interface for HTML and XML documents.
Q45: What is event bubbling and capturing?
A45: Event bubbling propagates events from child to parent, while capturing is from parent to child.
Q46: What is a generator function?
A46: A generator function can pause and resume execution using the 'function*' syntax and 'yield'
keyword.
Q47: What is a Set in JavaScript?
A47: A Set is a collection of unique values.
Q48: What is a Map in JavaScript?
A48: A Map is a collection of key-value pairs where keys can be of any type.
Q49: What is JSON?
A49: JSON (JavaScript Object Notation) is a lightweight format for storing and transporting data.
Q50: What are default parameters?
A50: Default parameters allow function parameters to have default values if no value is passed.
Q51: What is typeof operator?
A51: The 'typeof' operator returns a string indicating the type of the operand.