Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
JavaScript for Frontend Interviews
  1. 1.Introduction to JavaScript & Setup
  2. 2.Variables and Data Types
  3. 3.Operators and Expressions
  4. 4.Control Flow: Conditionals
  5. 5.Control Flow: Loops
  6. 6.Functions: Basics
  7. 7.Arrays and Objects
  8. 8.Higher-Order Functions & Callbacks
  9. 9.Scope and Closures
  10. 10.`this` Keyword and Context
  11. 11.Asynchronous JavaScript: Callbacks & Promises
  12. 12.ES6+ Features (Part 1)
  13. 13.ES6+ Features (Part 2)
  14. 14.Error Handling & Debugging
  15. 15.Asynchronous JavaScript: Async/Await
  16. 16.Prototypal Inheritance & `new` Keyword
  17. 17.Event Loop, Microtasks & Macrotasks
  18. 18.Working with the DOM
  19. 19.Advanced Array Methods & Data Structures
  20. 20.Frontend Interview Prep & Best Practices
JavaScript for Frontend Interviews
02

JavaScript · Lesson 2 of 20 · 3 min

Variables and Data Types

This topic is foundational. Answering these questions cleanly shows you have the basics covered, so the interview can move on to more interesting problems.

Declare variables with let and const, not var

In an interview, you will be asked about the difference between let, const, and var. Your answer should be about scope.

let and const are block-scoped. They exist only within the nearest set of curly braces ({...}), like in an if statement or a for loop. var is function-scoped, which means it "leaks" out of blocks and can cause unexpected behavior. Modern JavaScript uses let and const exclusively. Never use var in new code.

Use const by default for everything. This prevents accidental reassignment. Only switch to let if you know a variable needs to be reassigned, like a counter in a loop.

The trap: An interviewer might ask why the code below works.

const user = { name: "Alice" };
user.name = "Bob"; // This is allowed
console.log(user.name); // "Bob"

const makes the binding immutable, not the value. For a primitive type like a number, this means the value can't change. For a reference type like an object, this means the variable user will always point to the same object in memory, but the properties inside that object are still mutable.

Distinguish primitive vs. reference types

This is the single most important concept in this lesson. Getting it wrong is the source of countless bugs and a red flag in an interview. JavaScript has seven primitive types: String, Number, Boolean, null, undefined, Symbol, and BigInt. Everything else is an object (including arrays and functions).

Primitives are passed by value. When you assign a primitive to another variable, the value is copied.

Reference types (objects) are passed by reference. When you assign an object to another variable, you are copying the memory address (the reference), not the object itself. Both variables now point to the same object.

This difference is critical inside functions. Modifying an object passed into a function will affect the original object outside the function. Modifying a primitive will not.

// Primitive (pass-by-value)
let myScore = 100;
function increaseScore(score) {
  score = score + 10; // 'score' is a copy
}
increaseScore(myScore);
console.log(myScore); // 100 (original is unchanged)

// Reference (pass-by-reference)
const userProfile = { name: "Alice", score: 100 };
function addAdminRole(user) {
  user.isAdmin = true; // 'user' points to the same object
}
addAdminRole(userProfile);
console.log(userProfile); // { name: 'Alice', score: 100, isAdmin: true } (original is mutated)

Explain the quirks of typeof, null, and undefined

The typeof operator returns a string indicating the type of a value. It's straightforward for most primitives.

console.log(typeof "hello");   // "string"
console.log(typeof 42);        // "number"
console.log(typeof true);      // "boolean"
console.log(typeof undefined); // "undefined"

The trap: typeof has two major quirks you must know.

  1. typeof null returns "object". This is a famous, unfixable bug from the first version of JavaScript. To check for null, always use strict equality: myVar === null.
  2. typeof returns "object" for both objects and arrays. To check if a value is an array, use Array.isArray(myVar).

You should also be able to explain the difference between null and undefined.

  • undefined means a variable has been declared but not assigned a value. It's the default state.
  • null is an intentional assignment. It means "this variable has no value." Think of it as an explicit "nothing."

An interviewer is listening for this distinction: undefined is accidental absence of value, null is intentional.

Try it

In your editor, declare a const variable named config and assign it an object with a few properties (e.g., { theme: 'dark', version: '1.0' }). First, try to reassign the entire config variable to a new object. Observe the error. Next, modify one of the properties on the original config object (e.g., config.version = '1.1'). Log the object to the console to confirm it changed. Write a one-sentence comment in your code explaining why one action failed and the other succeeded.

Next: Operators and Expressions

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Introduction to JavaScript & Setup

Next

Operators and Expressions