DEV Community

Karen Molina
Karen Molina

Posted on Edited on

Frontend interview prep: variables, data types, scope and hoisting in JavaScript

Updated in 2026. I first wrote this post in 2021, while preparing for interviews. Thousands of you read it, so I rewrote it with everything I've learned since as a frontend engineer: clearer explanations, corrected details and model answers for interviews.

Variables, data types, scope and hoisting show up in almost every JavaScript interview. Not because they're hard, but because they reveal whether you understand how the language actually works, or just how to make it run.

Let's go one by one, and finish each section with how to answer it in an interview.

Data types

JavaScript has two families of types: primitives and objects.

Primitives

Primitives are simple, immutable values. You can't change a primitive; you can only replace it with another one. There are seven:

Type What it represents Example
string Text "Hola olla"
number Integers and decimals (64-bit floating point) 42, 3.14
bigint Integers larger than number can safely represent 9007199254740993n
boolean True or false true, false
undefined A declared variable with no value assigned let x;
null The intentional absence of a value let user = null;
symbol A unique identifier, often used as an object key Symbol("id")

Two details interviewers love:

0.1 + 0.2                 // 0.30000000000000004 (floating point)
Number.MAX_SAFE_INTEGER   // 9007199254740991, which is 2^53 - 1
2n ** 64n                 // 18446744073709551616n (bigint handles it)

typeof null               // "object" (a historical bug, null is a primitive)
Enter fullscreen mode Exit fullscreen mode

Objects

Everything that isn't a primitive is an object: plain objects, arrays, functions, dates, maps, sets. Objects are mutable and are handled by reference (more on that below).

Interview answer: "JavaScript has seven primitive types (string, number, bigint, boolean, undefined, null and symbol), which are immutable and copied by value. Everything else is an object, which is mutable and shared by reference. A classic gotcha is that typeof null returns 'object'."

Variables: var, let and const

var let const
Scope Function Block { } Block { }
Can be reassigned Yes Yes No
Can be redeclared in the same scope Yes No No
Accessed before declaration undefined ReferenceError ReferenceError

var

The original way, before ES6 (2015). The problem isn't that you can reassign it (let can too). It's that var:

  • can be redeclared silently,
  • ignores block scope and only respects function scope,
  • is available before its declaration, with the value undefined.
var greeting = "Hola olla";
var greeting = "Hi!"; // no error: you just overwrote it

if (true) {
  var secret = "leaks out";
}
console.log(secret); // "leaks out"
Enter fullscreen mode Exit fullscreen mode

let

Introduced in ES6. It can be reassigned but not redeclared, and it only exists inside the block where you declare it.

let greeting = "Hola olla";
greeting = "Hi!";      // fine

if (true) {
  let secret = "stays here";
}
console.log(secret);   // ReferenceError: secret is not defined
Enter fullscreen mode Exit fullscreen mode

Redeclaring it in the same scope is a SyntaxError, which means the whole script fails to run, not just that line.

const

Can't be reassigned or redeclared. But here's the catch interviewers look for: const doesn't make a value immutable. It only prevents the variable from pointing to a different value.

const cat = { name: "SimĂłn" };

cat = { name: "Tito" }; // TypeError: Assignment to constant variable.
cat.name = "Tito";      // works: same object, different property

const fruits = ["Apple", "Pear"];
fruits.push("Melon");   // works too
Enter fullscreen mode Exit fullscreen mode

If you need a truly frozen object, use Object.freeze() (shallow) or a library for deep immutability.

Interview answer: "var is function-scoped, can be redeclared and is initialized as undefined when hoisted. let and const are block-scoped and can't be redeclared. const can't be reassigned, but if it holds an object, the object itself can still be mutated. In practice I use const by default, let when I need to reassign, and avoid var."

Scope

Scope defines where a variable can be accessed.

  • Global scope: declared outside any function, block or module. Accessible everywhere. Keep it to a minimum.
  • Function scope: variables declared inside a function only exist inside it. This is sometimes called local scope.
  • Block scope: with let and const, variables declared inside { } (an if, a for) only exist there.
  • Module scope: in an ES module, top-level variables are not global. They only exist in that file, and other files can only use what you explicitly export.
const global = "everyone sees me";

function greet() {
  const inFunction = "only inside greet";

  if (true) {
    const inBlock = "only inside the if";
    console.log(global, inFunction, inBlock); // all visible
  }

  console.log(inBlock); // ReferenceError
}
Enter fullscreen mode Exit fullscreen mode

The key rule: inner scopes can see outward, but outer scopes can't see inward. That chain of lookups is called the scope chain, and it's also what makes closures possible.

Interview answer: "Scope determines where a variable is accessible. JavaScript has global, function, block and module scope. When a variable is used, the engine looks for it in the current scope and then moves outward through the scope chain. Inner scopes can access outer variables, never the other way around."

Hoisting

Hoisting is one of the most misunderstood parts of JavaScript, partly because of how it's usually explained.

You'll often hear that "declarations are moved to the top." The code doesn't actually move. What happens is that before running your code, the engine goes through a creation phase where it registers every declaration in memory for its scope. That's why some things can be used before the line where they're written.

But each kind of declaration behaves differently:

console.log(a);   // undefined
var a = 1;

console.log(b);   // ReferenceError: Cannot access 'b' before initialization
let b = 2;

console.log(greet()); // "hi"
function greet() { return "hi"; }

sayBye();         // TypeError: sayBye is not a function
var sayBye = function () { return "bye"; };
Enter fullscreen mode Exit fullscreen mode

What's going on:

Declaration Hoisted? Value before its line
var Yes undefined
let / const Yes None: accessing it throws a ReferenceError
Function declaration Yes, completely The full function, so you can call it
Function expression with var Only the variable undefined, so calling it throws a TypeError
class Yes None, like let

The temporal dead zone

let, const and class are hoisted, but they aren't initialized. The time between the start of the scope and the line where they're declared is called the temporal dead zone (TDZ). Touching the variable in that zone throws a ReferenceError.

The TDZ is a feature, not a bug: it catches the "used before defined" mistakes that var silently turns into undefined.

Interview answer: "Hoisting means declarations are registered in memory before the code runs, so they exist from the start of their scope. var is initialized as undefined, function declarations are fully available, and let, const and classes are hoisted but stay uninitialized in the temporal dead zone until their declaration, so accessing them early throws a ReferenceError. The code itself doesn't move."

Bonus: value vs. reference

This one causes real bugs, especially in React.

// Primitives are copied by value
let a = 1;
let b = a;
a = 2;
console.log(b); // 1

// Objects share a reference
const fruit = { name: "Strawberry" };
const sameFruit = fruit;
fruit.name = "Apple";
console.log(sameFruit.name); // "Apple"

// If you need an independent copy
const shallowCopy = { ...fruit };
const deepCopy = structuredClone(fruit);
Enter fullscreen mode Exit fullscreen mode

That's also why React state shouldn't be mutated: if you change the same object, the reference doesn't change, and React may not re-render.

Quick checklist before your interview

  • [ ] Can you list the seven primitives?
  • [ ] Can you explain why typeof null is "object"?
  • [ ] Can you explain the difference between var, let and const in one sentence each?
  • [ ] Can you show that const objects can still change?
  • [ ] Can you describe the scope chain?
  • [ ] Can you explain hoisting without saying "the code moves to the top"?
  • [ ] Do you know what the temporal dead zone is?

Next in the series: closures, and why they come up in almost every JavaScript interview.

Which of these concepts confused you the most when you started? Let me know in the comments 👇

Top comments (3)

Collapse
 
gfierro profile image
Greg Fierro • • Edited

Hi Karen ! Fantastic article. Keep up the good work in contributing your knowledge of JS.
I have to agree though with Derrick's comment about proof reading articles before submitting them for publication. You are not alone! I have noticed a large percentage of articles her in dev.to need major redactions. This may be due to English not being the author's primary language. I totally understand this. So I volunteer to proof read any future articles you might want to contribute. :-)

Collapse
 
drex7 profile image
Derrick Asamoah •

Great article, but it'd be better if you let someone proofread your articles before posting it.

Collapse
 
voidrizoma profile image
Karen Molina •

Hi Derrick!
Thanks for the feedback! :D