Javascript Interview Questions

JavaScript Interview Questions - Fundamentals

50 essential JavaScript interview questions covering variables, data types, operators, type coercion, scope, hoisting, and basic syntax.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

50 essential JavaScript interview questions covering variables, data types, operators, type coercion, scope, hoisting, and basic syntax. This interview-focused guide covers essential javascript interview questions - fundamentals concepts for technical interviews.

JavaScript Interview Questions – Fundamentals

Master the absolute essentials. These 50 questions cover variables, data types, type coercion, scope fundamentals, hoisting, and core syntax rules — the foundation every JavaScript developer must know.


1. What are the different data types in JavaScript?

JavaScript has two categories of data types:

Primitive Types (7): string, number, bigint, boolean, undefined, null, symbol.

Non-Primitive: object — collections of key-value pairs, arrays, functions, dates, etc.

[!NOTE] typeof null returns "object" — this is a well-known JavaScript bug from the very first implementation of JS. null is actually a primitive type.

2. What is the difference between var, let, and const?

  • var: Function-scoped, hoisted (initialized to undefined), can be redeclared
  • let: Block-scoped, hoisted but NOT initialized (TDZ), cannot be redeclared
  • const: Block-scoped, must be initialized at declaration, cannot be reassigned

[!IMPORTANT] const prevents reassignment of the variable, but NOT mutation of the object it points to. const arr = []; arr.push(1); works fine.

3. What is type coercion?

Type coercion is JavaScript's automatic conversion of values from one type to another. JavaScript is a weakly typed language, so it automatically converts types in certain operations.

console.log(5 + "5");     // "55" (number coerced to string)
console.log("5" - 3);     // 2 (string coerced to number)
console.log(true + 1);    // 2 (boolean coerced to number)
console.log([] + {});     // "[object Object]"
console.log([] == false); // true

4. What's the difference between == and ===?

  • == (loose equality) — performs type coercion before comparison
  • === (strict equality) — compares both value and type without coercion
console.log(5 == "5");    // true (coerced)
console.log(5 === "5");   // false (different types)
console.log(null == undefined);  // true
console.log(null === undefined); // false

[!TIP] Always use === unless you have a specific, intentional reason for loose equality.

5. What is hoisting?

Hoisting is JavaScript's default behavior of moving declarations to the top of their scope during compilation. Variables declared with var are hoisted and initialized to undefined. let and const are hoisted but NOT initialized (TDZ).

console.log(x); // undefined (not ReferenceError!)
var x = 5;

// Function declarations are fully hoisted
sayHello(); // Works!
function sayHello() { console.log("Hello"); }

6. What's the difference between undefined and null?

  • undefined — variable has been declared but not assigned a value (JavaScript default)
  • null — intentional absence of any value (developer-assigned)

[!NOTE] typeof null returns "object" (a JS bug). typeof undefined returns "undefined".

7. What is the scope chain?

The scope chain determines how JavaScript looks up variable values. When accessing a variable, JS starts at the current scope and moves outward through parent scopes until it finds the variable or reaches the global scope.

8. What is NaN and how do you check for it?

NaN stands for "Not a Number" but is technically of type number. It represents the result of an invalid mathematical operation.

console.log(typeof NaN);         // "number"
console.log(0 / 0);              // NaN
console.log(Number.isNaN(NaN));  // true (preferred)
console.log(isNaN("hello"));     // true (but coerces — avoid!)

[!TIP] Number.isNaN() is preferred over isNaN() because it doesn't coerce the value first.

9. What is the difference between undefined and not defined?

  • undefined: Variable exists in memory but has no value assigned
  • not defined / ReferenceError: Variable was never declared
let x;
console.log(x);  // undefined
console.log(z);  // ReferenceError: z is not defined

10. How does typeof work?

typeof is a unary operator that returns a string indicating the type of the operand.

typeof "hello";      // "string"
typeof 42;           // "number"
typeof true;         // "boolean"
typeof undefined;    // "undefined"
typeof null;         // "object" (known bug)
typeof {};           // "object"
typeof [];           // "object"
typeof function(){}; // "function"
typeof Symbol();     // "symbol"

11. What is the Temporal Dead Zone (TDZ)?

The TDZ is the period between entering a scope and the actual declaration of a let/const variable. Accessing the variable during TDZ throws a ReferenceError.

[!IMPORTANT] var doesn't have TDZ — it's initialized to undefined during hoisting.

12. What is a variable declaration vs assignment?

  • Declaration: let x; — tells JS a variable exists
  • Assignment: x = 5; — gives the variable a value
  • Initialization: let x = 5; — both at once

13. What are JavaScript keywords and reserved words?

Keywords are words with special meaning in JS (if, for, while, class, function, return, new, this, etc.). They cannot be used as identifiers (variable names, function names).

14. What are identifiers in JavaScript?

Identifiers are names used to identify variables, functions, objects, and classes. Rules:

  • Must start with a letter, underscore _, or dollar sign $
  • Cannot start with a digit
  • Can contain letters, digits, underscores, and dollar signs
  • Case-sensitive (myVarmyvar)

15. What is the difference between single, double, and backtick quotes?

  • Single (') and double (") quotes — identical behavior, personal preference
  • Backticks (`) — template literals, support string interpolation and multi-line
const name = "Alice";
console.log(`Hello, ${name}!`); // Template literal

16. What are truthy and falsy values?

Falsy values (6 total): false, 0, "" (empty string), null, undefined, NaN.

Truthy values: Everything else — including [], {}, "0", "false", Infinity.

if ([]) console.log("Empty array is truthy!"); // prints
if ("0") console.log("String zero is truthy!"); // prints

17. What is the difference between null and 0?

  • null means no value (intentional)
  • 0 is a valid numeric value
console.log(null == 0);  // false
console.log(null > 0);   // false
console.log(null >= 0);  // true (weird coercion!)

18. What is BigInt?

BigInt is a numeric primitive that can represent integers larger than 2^53 - 1 (the Number limit). Created by appending n or using BigInt().

const huge = 9007199254740991n;
const big = BigInt("9007199254740991");

19. What is the difference between parseInt(), parseFloat(), and Number()?

  • Number() — converts to a number, returns NaN for invalid
  • parseInt() — parses integer, stops at first non-digit, accepts radix
  • parseFloat() — parses floating-point number
Number("42abc");       // NaN
parseInt("42abc");     // 42
parseFloat("3.14px");  // 3.14

[!TIP] Always pass radix to parseInt(): parseInt("10", 2) = 2 (binary), parseInt("10") = 10.

20. What is the + unary operator?

The + before a value converts it to a number.

console.log(+"42");    // 42
console.log(+true);    // 1
console.log(+false);   // 0
console.log(+"");      // 0
console.log(+"hello"); // NaN

21. What are the bitwise operators in JavaScript?

& (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift), >>> (unsigned right shift). They operate on 32-bit signed integers.

22. What is the comma operator?

The comma operator evaluates both operands and returns the value of the last one.

let x = (1, 2, 3); // x = 3

23. What is the difference between ++x and x++?

  • ++x (pre-increment): increments x, then returns the new value
  • x++ (post-increment): returns the current value, then increments x
let a = 5;
console.log(++a); // 6
console.log(a++); // 6 (returns old value, then increments to 7)

24. What are the comparison operators?

==, ===, !=, !==, >, <, >=, <=.

25. What are the logical operators?

&& (AND), || (OR), ! (NOT), ?? (nullish coalescing).

[!TIP] || returns the first truthy value, ?? returns the first defined value. 0 || 10 = 10, but 0 ?? 10 = 0.

26. What is the ternary operator?

A shorthand for if-else: condition ? exprIfTrue : exprIfFalse.

const status = age >= 18 ? "Adult" : "Minor";

27. What are blocks in JavaScript?

A block statement {} groups zero or more statements. It creates a new scope for let and const but NOT for var.

28. What is the difference between break and continue?

  • break — exits the current loop entirely
  • continue — skips the current iteration and continues with the next

29. What is a switch statement?

Evaluates an expression and matches its value against case clauses. Use break to prevent fall-through.

switch (day) {
    case "Monday": console.log("Start of week"); break;
    case "Friday": console.log("TGIF!"); break;
    default: console.log("Midweek");
}

30. What is the difference between for, while, and do...while?

  • for — known number of iterations
  • while — condition checked before each iteration
  • do...while — executes at least once, condition checked after

31. What are labels in JavaScript?

Labels provide a statement with an identifier that can be used with break or continue to control nested loops.

outer: for (let i = 0; i < 3; i++) {
    for (let j = 0; j < 3; j++) {
        if (i === 1 && j === 1) break outer;
    }
}

32. What is the in operator?

Checks if a property exists in an object (including prototype chain).

console.log("length" in []);     // true
console.log("toString" in {});   // true (inherited)

33. What is the instanceof operator?

Tests whether an object's prototype chain contains the prototype of a constructor.

console.log([] instanceof Array);  // true
console.log([] instanceof Object); // true

34. What is the delete operator?

Removes a property from an object. Does NOT affect variables or functions.

const obj = { a: 1, b: 2 };
delete obj.a;
console.log(obj); // { b: 2 }

35. What is the spread operator (...)?

Expands an iterable (array, string, object) into individual elements.

const arr = [1, 2, 3];
console.log(...arr);         // 1 2 3
const copy = [...arr, 4, 5]; // [1, 2, 3, 4, 5]

36. What is destructuring?

Extracts values from arrays or properties from objects into distinct variables.

const [first, second] = [10, 20];
const { name, age } = { name: "Alice", age: 25 };

37. What are template literals?

String literals using backticks that allow embedded expressions (${expression}) and multi-line strings.

38. What are tagged templates?

A function that processes a template literal. The first argument is an array of string literals, remaining arguments are the substitution values.

function tag(strings, ...values) {
    return strings.reduce((r, s, i) => r + s + (values[i] || ""), "");
}
console.log(tag`Hello ${"World"}!`); // "Hello World!"

39. What is a semicolon in JavaScript?

Semicolons are optional in JavaScript due to ASI (Automatic Semicolon Insertion). However, relying on ASI can cause bugs.

[!CAUTION] Lines starting with (, [, `, +, -, / may not get automatic semicolons before them. Always use semicolons to be safe.

40. What is strict mode ("use strict")?

Enables a restricted variant of JavaScript that catches common coding mistakes and prevents unsafe actions.

"use strict";
x = 5; // ReferenceError: x is not defined

41. What are the rules for variable naming?

  • Cannot start with a digit
  • Cannot contain spaces or hyphens
  • Cannot be a reserved keyword
  • Case-sensitive
  • Convention: camelCase for variables, PascalCase for classes

42. What is a literal?

A literal is a fixed value directly written in code: 42, "hello", true, [1, 2], {a: 1}.

43. What is an expression vs statement?

  • Expression: Produces a value (5 + 5, x * 2)
  • Statement: Performs an action (if, for, let x = 5)

44. What is the difference between primitive and reference types?

  • Primitive: Stored by value, compared by value, immutable
  • Reference (objects): Stored by reference, compared by reference, mutable
let a = 5, b = a; a = 10; console.log(b); // 5 (copied by value)
let c = [1], d = c; c.push(2); console.log(d); // [1, 2] (shared reference)

45. What is the global object?

In browsers, it's window. In Node.js, it's global. globalThis provides a standardized way to access it.

46. What is console?

The built-in object providing browser debugging console access: console.log(), console.error(), console.table(), console.time().

47. What are comments in JavaScript?

  • Single-line: // comment
  • Multi-line: /* comment */

48. What is Hungarian notation?

A naming convention where variable names include a prefix indicating their type (e.g., bBusy for boolean, strName for string). Not common in modern JavaScript.

49. What is eval()?

Executes a string as JavaScript code. Never use it — it's a security risk and performance killer.

[!CAUTION] eval() is considered one of the most dangerous functions in JavaScript. It allows arbitrary code execution and prevents optimizations.

50. What is with statement?

Extends the scope chain for a statement. Deprecated and forbidden in strict mode. Never use it.