JavaScript Interview Questions and Answers
Q: What is JavaScript?
A: JavaScript is a high-level, interpreted programming language used to create dynamic and
interactive effects within web browsers.
Q: Difference between var, let, and const.
A: var is function-scoped, let and const are block-scoped. var allows re-declaration, let allows
reassignment but no re-declaration, const allows neither.
Q: What are data types in JavaScript?
A: Primitive types: string, number, boolean, null, undefined, symbol, bigint. Non-primitive: object,
array, function.
Q: What is hoisting?
A: Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope.
Q: Difference between == and ===.
A: `==` compares values with type coercion, `===` compares both value and type.
Q: What are first-class functions?
A: Functions in JavaScript are treated as first-class citizens, meaning they can be assigned to
variables, passed as arguments, and returned from other functions.
Q: What is a callback function?
A: A callback is a function passed into another function as an argument to be executed later.
Q: What is a closure?
A: A closure is a function that remembers its outer variables and can access them even when the
outer function has finished executing.
Q: Difference between function declaration and function expression?
A: Function declarations are hoisted, function expressions are not. Declarations define a named
function, expressions can be anonymous.
Q: What is lexical scope?
A: Lexical scope refers to the fact that in JavaScript, scope is determined by the position of functions
and blocks in the code.
Q: How do you clone an object?
A: Use Object.assign({}, obj), spread operator {...obj}, or structuredClone(obj) for deep cloning.
Q: Difference between shallow and deep copy?
A: Shallow copy copies only the top-level properties. Deep copy copies nested objects as well.
Q: How to merge two arrays?
A: Use concat() or spread operator like [...arr1, ...arr2].
Q: Ways to iterate over an array?
A: for loop, forEach(), map(), for...of, while loop.
Q: What is the event loop?
A: An event loop handles asynchronous callbacks in JavaScript, managing the call stack and
message queue.
Q: What is asynchronous programming?
A: A programming model where operations occur independently of the main program flow.
Q: Explain Promises.
A: A Promise represents a value which may be available now, later, or never. Used for handling
async operations.
Q: What is async/await?
A: Syntactic sugar over Promises, allowing asynchronous code to be written in a synchronous style.
Q: What are arrow functions?
A: Arrow functions are shorter syntax for writing functions and do not have their own `this`.
Q: What is the DOM?
A: DOM (Document Object Model) is a programming interface for HTML and XML documents. It
represents the structure of a web page.
Q: How to select elements in the DOM?
A: Use methods like getElementById, querySelector, querySelectorAll, getElementsByClassName,
etc.
Q: What is event bubbling and capturing?
A: Bubbling: event propagates from target to root. Capturing: event propagates from root to target.
Q: What are setTimeout() and setInterval()?
A: setTimeout executes a function once after a delay. setInterval repeats it at intervals.
Q: What is destructuring?
A: Destructuring allows extracting values from arrays or objects into distinct variables.
Q: What are template literals?
A: Template literals are strings allowing embedded expressions using backticks and `${}` syntax.
Q: What is the spread operator and rest parameter?
A: Spread expands elements (e.g., in arrays), rest gathers them into an array (e.g., function
arguments).
Q: Use of map(), filter(), and reduce()?
A: map transforms each array element, filter selects elements, reduce combines elements into a
single value.
Q: Difference between null and undefined?
A: null is intentional absence of value, undefined is a variable declared but not assigned.
Q: What is NaN?
A: NaN stands for 'Not-a-Number', a value representing an unrepresentable or undefined numerical
result.
Q: Use of typeof and instanceof?
A: `typeof` returns the type of a variable, `instanceof` checks if an object is an instance of a
constructor.