Complete Interview Questions & Answer Bank
Full collection of 50 JavaScript interview questions ranging from core fundamentals and coding challenges to advanced engine internals.
Q1: What are the primitive data types in JavaScript vs objects?
JavaScript has 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null. Primitives are immutable and passed by value. Objects (including arrays, functions, and plain objects) are mutable collections of key-value properties and are passed by reference.
- Primitive types are immutable and stored directly by value.
- Objects are mutable reference types stored by memory location.
What does typeof null return in JavaScript?
Q2: What is the difference between var, let, and const?
var is function-scoped (or globally scoped) and hoisted with an initial value of undefined. let and const are block-scoped and hoisted into a Temporal Dead Zone (TDZ). const creates a read-only variable reference that cannot be reassigned after declaration.
varis function-scoped;letandconstare block-scoped.letandconstexist in the Temporal Dead Zone prior to declaration.
What error occurs when accessing a let variable before its declaration line?
Q3: What is the difference between == and === in JavaScript?
== (loose equality) performs type coercion to convert operands to a common type before comparing. === (strict equality) checks for equality without type conversion; both the value and the type must match.
==converts operands to the same type before comparison.===compares both value and data type without coercion.
What is the result of the comparison: [] == false?
Q4: How does Hoisting work in JavaScript?
Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their containing scope during creation phase. Function declarations are hoisted with their complete definitions, var declarations are hoisted with undefined, while let and const remain uninitialized in the TDZ.
- Function declarations are hoisted with their definitions fully accessible.
varis hoisted asundefined;let/constare placed in the TDZ.
What is printed when calling foo() declared as: var foo = function() { return 1; }; before assignment?
Q5: What is Scope and the Scope Chain?
Scope determines the accessibility of variables in code. JavaScript has Global Scope, Function Scope, and Block Scope. When a variable is referenced, JavaScript looks in the current scope; if not found, it traverses up the Scope Chain to outer enclosing scopes until it reaches the global scope.
- Lexical scope means inner scopes can access outer scope variables.
- The scope chain lookup travels outward up to global scope.
Where does lexical scope determine variable accessibility from?
Q6: What is a Closure in JavaScript?
A closure is the combination of a function bundled together with references to its surrounding lexical environment. Closures give inner functions access to an outer function's scope variables even after the outer function has finished executing.
- Functions retain references to variables in their lexical environment.
- Powers state encapsulation, module patterns, and memoization.
What will console.log print: for(var i=0; i<3; i++) { setTimeout(() => console.log(i), 100); }?
Q7 [Coding]: Implement a counter function using closures that supports increment, decrement, and getValue.
function createCounter(initialValue = 0) {
let count = initialValue;
return {
increment: () => ++count,
decrement: () => --count,
getValue: () => count
};
}
This encapsulates count inside a private closure, exposing only controlled modifier functions.
- Enables private variables without native class private fields.
- Exposes object methods that maintain access to closed-over state.
Can code outside createCounter access or modify the internal count variable directly?
Q8: How does the `this` keyword work in different execution contexts?
The value of this depends on how a function is called: standard method call (obj.fn()) sets this to obj; plain function call sets this to window/undefined (strict mode); constructors (new Fn()) bind this to the new instance; arrow functions bind this lexically from their outer scope.
- Standard function
thisis bound dynamically at call time. - Arrow function
thisis determined statically from lexical outer scope.
What is `this` inside an arrow function defined within an object method?
Q9: What is the difference between call(), apply(), and bind()?
call() invokes a function with a specified this context and arguments passed individually (fn.call(ctx, arg1, arg2)). apply() invokes the function with arguments passed as an array (fn.apply(ctx, [arg1, arg2])). bind() returns a new function with this bound permanently.
callaccepts arguments comma-separated;applyaccepts an array.bindreturns a bound copy of the function without immediate execution.
Which method executes a function immediately with an array of arguments?
Q10: How does Prototypal Inheritance work in JavaScript?
Objects in JavaScript have an internal [[Prototype]] link (exposed as __proto__). When accessing a property on an object, JavaScript looks at the object first, then traverses up the prototype chain until the property is found or null is reached.
- Objects inherit properties and methods dynamically via prototype delegation.
- Top of the prototype chain ends at
Object.prototype.__proto__ === null.
What method creates a new object using an existing object as its prototype?