Module 11: JavaScript Interview Questions and Answers
Core JavaScript Interview Questions and Answers
Review equality, types, declarations, coercion, and core language behaviour.
Use these questions to check whether you can explain JavaScript clearly, not simply repeat definitions. Try answering each one before reading the answer.
1. What is JavaScript?#
Answer: JavaScript is a high-level, dynamically typed programming language standardized as ECMAScript. It runs in browsers and in non-browser environments such as Node.js. The available host APIs differ between environments.
2. Is JavaScript the same as Java?#
Answer: No. They are separate languages with different type systems, runtime environments, and design histories. Their names are similar, but JavaScript is not a simplified form of Java.
3. What is the difference between let, const, and var?#
Answer: let and const are block-scoped. let permits reassignment; const does not permit reassignment of the binding. var is function-scoped and has older redeclaration and initialization behaviour. Prefer const by default and let when reassignment is needed.
4. What are primitive types?#
Answer: The primitive types are string, number, bigint, boolean, undefined, symbol, and null. Objects, including arrays and functions, are non-primitive values.
5. What is the difference between == and ===?#
Answer: == can coerce operands before comparison. === compares without that coercion. Strict equality is the clearer default for most application code.
6. Why does typeof null return "object"?#
Answer: This is a long-standing historical quirk in JavaScript that remains for compatibility. Check for null with value === null rather than relying on typeof alone.
7. What is NaN?#
Answer: NaN represents an invalid numeric result, such as converting a non-numeric string with Number. Use Number.isNaN(value) to check specifically for it. NaN is unequal to every value, including itself.
8. What are truthy and falsy values?#
Answer: JavaScript converts values to booleans in conditions. Values such as false, 0, "", null, undefined, and NaN are falsy. Empty arrays and objects are truthy.
9. What is the difference between null and undefined?#
Answer: undefined commonly signals an unassigned or missing value; null commonly represents an intentional absence. The exact convention depends on the application, so consistency matters.
10. What is type coercion?#
Answer: Coercion is automatic or implicit conversion between types during an operation. For example, "5" + 1 produces the string "51", while "5" - 1 produces the number 4. Explicit conversion makes intent clearer.
11. What is the difference between || and ???#
Answer: || uses the fallback when the left value is any falsy value. ?? uses the fallback only for null or undefined, preserving valid values such as zero and an empty string.
12. What is immutability?#
Answer: Immutability means a value or data structure is not changed after creation. JavaScript does not make ordinary objects immutable by default, so code often creates updated copies instead of mutating shared state. A spread copy is shallow, not deep.