JavaScript Interview Questions and Answers
JavaScript Interview Questions and Answers
Q: What is JavaScript?
A: var is function-scoped, let and const are block-scoped. var allows re-declaration, let allows
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.
A: `==` compares values with type coercion, `===` compares both value and type.
A: Functions in JavaScript are treated as first-class citizens, meaning they can be assigned to
Q: What is a closure?
A: A closure is a function that remembers its outer variables and can access them even when the
A: Function declarations are hoisted, function expressions are not. Declarations define a named
function, expressions can be anonymous.
A: Lexical scope refers to the fact that in JavaScript, scope is determined by the position of functions
A: Use Object.assign({}, obj), spread operator {...obj}, or structuredClone(obj) for deep cloning.
A: Shallow copy copies only the top-level properties. Deep copy copies nested objects as well.
A: An event loop handles asynchronous callbacks in JavaScript, managing the call stack and
message queue.
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.
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
etc.
A: Bubbling: event propagates from target to root. Capturing: event propagates from root to target.
Q: What is destructuring?
A: Destructuring allows extracting values from arrays or objects into distinct variables.
A: Template literals are strings allowing embedded expressions using backticks and `${}` syntax.
A: Spread expands elements (e.g., in arrays), rest gathers them into an array (e.g., function
arguments).
A: map transforms each array element, filter selects elements, reduce combines elements into a
single value.
A: null is intentional absence of value, undefined is a variable declared but not assigned.
Q: What is NaN?
result.
Q: Use of typeof and instanceof?
constructor.