JavaScript Fundamentals Interview Questions and Answers
15 core JavaScript questions: var, let and const, types, equality, closures, this, prototypes, and copying objects.
1. What is the difference between var, let, and const?
var is function scoped, can be redeclared, and is hoisted and initialized as undefined. let and const are block scoped and live in the temporal dead zone until their declaration runs, so using them early throws a ReferenceError. const prevents reassignment of the binding, but an object it holds can still be mutated. Prefer const, use let when you must reassign, and avoid var.
2. What is hoisting?
Declarations are processed before code runs. Function declarations are hoisted with their body, so you can call them before they appear. var variables are hoisted and set to undefined. let, const, and class are hoisted but not initialized, which causes the temporal dead zone.
console.log(a); // undefined var a = 1; console.log(b); // ReferenceError let b = 2;3. What are the JavaScript data types?
Seven primitives: string, number, bigint, boolean, undefined, null, and symbol. Everything else is an object (including arrays, functions, dates, maps). Primitives are immutable and compared by value; objects are compared by reference. typeof null returns 'object', a long-standing quirk, so check null explicitly.
4. What is the difference between == and ===?
=== is strict equality: no type conversion, so 5 === '5' is false. == converts types first using coercion rules, which gives surprising results such as 0 == '' being true and null == undefined being true. Use === by default; the only common use of == is value == null to check for both null and undefined.
5. What are truthy and falsy values?
In a boolean context these are falsy: false, 0, -0, 0n, '' (empty string), null, undefined, and NaN. Everything else is truthy, including '0', 'false', [] and {}. Be careful with checks like if (count) when 0 is a valid value; use count !== undefined or the nullish coalescing operator instead.
6. What is the difference between null and undefined?
undefined means a variable has been declared but not assigned, or a property or argument is missing. null is an explicit value meaning no value, assigned deliberately by the programmer. Both are falsy, and null == undefined is true while null === undefined is false.
7. What is a closure?
A closure is a function together with the variables from the scope where it was created, which it keeps access to even after that outer function has returned. Closures enable private state, factories, and callbacks.
function makeCounter() { let count = 0; return function () { count += 1; return count; }; } const next = makeCounter(); next(); // 1 next(); // 28. How does the this keyword work?
this depends on how a function is called, not where it is defined. In a method call it is the object before the dot. In a plain function call it is undefined in strict mode (the global object otherwise). With new it is the new object. call, apply, and bind set it explicitly. Arrow functions do not have their own this; they use the one from the surrounding scope.
9. What is the difference between call, apply, and bind?
All three set this for a function. call invokes it immediately with arguments listed one by one. apply invokes it immediately with arguments in an array. bind does not invoke it; it returns a new function with this (and optionally some arguments) fixed.
function greet(greeting) { return greeting + ', ' + this.name; } const user = { name: 'Ada' }; greet.call(user, 'Hello'); greet.apply(user, ['Hello']); const bound = greet.bind(user, 'Hello'); bound();10. What is prototypal inheritance?
Every object has an internal link to a prototype object. When you read a property that the object lacks, JavaScript looks along the prototype chain until it finds it or reaches null. Functions used with new set the prototype of the objects they create, and the class syntax is syntactic sugar over this mechanism.
11. What does the new keyword do?
It creates a new empty object, links its prototype to the constructor's prototype property, runs the constructor with this bound to the new object, and returns that object (unless the constructor returns another object).
12. What is the difference between a shallow copy and a deep copy? How do you make each?
A shallow copy duplicates only the top level, so nested objects are still shared. Use the spread operator or Object.assign. A deep copy duplicates nested structures too. Use structuredClone, which is built into modern browsers and Node.js and handles dates, maps, sets, and cycles. JSON.parse(JSON.stringify(x)) is a quick option but drops functions, undefined, and dates become strings.
const a = { n: 1, inner: { x: 1 } }; const shallow = { ...a }; const deep = structuredClone(a); shallow.inner.x = 99; // also changes a.inner.x13. What is the difference between const and Object.freeze?
const stops you from reassigning the variable. Object.freeze stops you from adding, removing, or changing the properties of an object, but only at the first level, and in non-strict code it fails silently. To make a value fully immutable, combine const with a deep freeze or use immutable data patterns.
14. What is NaN, and how do you check for it?
NaN means not a number and is the result of invalid numeric operations such as 0 / 0 or Number('abc'). It is the only value not equal to itself, so NaN === NaN is false. Use Number.isNaN(value) rather than the global isNaN, which coerces its argument first.
15. What is the difference between a function declaration and a function expression?
A declaration (function add() {}) is hoisted entirely, so it can be called before it appears. A function expression (const add = function () {} or an arrow function) is only created when execution reaches it, and a const or let binding is not usable before that. Expressions are also what you pass as callbacks.
Interview Tips
- Be ready to predict the output of small snippets; interviewers use them to test hoisting, closures, and this.
- When you explain a concept, finish with a tiny code example you can write from memory.
- Say why a pitfall happens (for example the temporal dead zone), not just that it happens.