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"
constmakes 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 variableuserwill 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.
typeof nullreturns"object". This is a famous, unfixable bug from the first version of JavaScript. To check fornull, always use strict equality:myVar === null.typeofreturns"object"for both objects and arrays. To check if a value is an array, useArray.isArray(myVar).
You should also be able to explain the difference between null and undefined.
undefinedmeans a variable has been declared but not assigned a value. It's the default state.nullis 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.