JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsjavascriptVariables
PrevNext
javascript
beginner
7 min read

Variables

es6
fundamentals
hoisting
scope
variables

var is function-scoped and hoisted with undefined, let is block-scoped and reassignable, const is block-scoped and cannot be reassigned — modern JavaScript favors const by default and let when reassignment is needed.

Key Points

1var

Function-scoped, hoisted with undefined, can be re-declared — causes scoping bugs in loops and blocks

2let

Block-scoped, hoisted but in TDZ, can be reassigned but not re-declared in the same scope

3const

Block-scoped, must be initialized, cannot be reassigned — but object/array contents can still be mutated

4Scope Difference

var ignores block boundaries (if/for); let and const are confined to the nearest {} block

5Best Practice

Default to const, use let for reassignment, avoid var — this prevents scoping bugs and signals intent

What You'll Learn

  • Understand the difference between var, let, and const in JavaScript
  • Know why const does not mean immutable for objects and arrays
  • Know the best practices for choosing between const, let, and var

Deep Dive

JavaScript has three variable declaration keywords: var (original), let and const (added in ES6/ES2015). Understanding their differences in scope, hoisting, and mutability is fundamental and one of the most common interview questions.

var

  • Function-scoped: a var declared inside a function is accessible anywhere within that function, regardless of block boundaries (if, for, etc.).
  • If declared outside any function, var creates a global variable and adds a property to the global object (window in browsers).
  • Can be re-declared in the same scope without error: var x = 1; var x = 2; is valid.
  • Hoisted and initialized to undefined — accessing before declaration returns undefined instead of throwing an error.
  • This behavior causes subtle bugs, especially in loops: a var in a for loop is shared across all iterations, which breaks closures inside the loop.

let

  • Block-scoped: limited to the nearest enclosing block ({} — if, for, while, or any curly-brace block).
  • Cannot be re-declared in the same scope — let x = 1; let x = 2; throws SyntaxError.
  • Can be reassigned: let x = 1; x = 2; is valid.
  • Hoisted but not initialized — accessing before declaration throws ReferenceError (Temporal Dead Zone).
  • Can be declared without initialization: let x; is valid (value is undefined until assigned).

const

  • Block-scoped, same as let.
  • Must be initialized at declaration: const x; throws SyntaxError.
  • Cannot be reassigned: const x = 1; x = 2; throws TypeError.
  • Important nuance: const prevents reassignment of the binding, not mutation of the value. A const object or array can still have its properties/elements modified: const arr = [1]; arr.push(2); is valid.
  • To make an object truly immutable, use Object.freeze() — but this is shallow (nested objects are still mutable).

Best Practices

  • Use const by default — it signals that the binding won't change, making code easier to reason about.
  • Use let only when you need to reassign (loop counters, accumulators, conditional assignments).
  • Avoid var entirely in modern code — it causes scoping bugs and is hoisted in confusing ways.
  • Declare variables as close to their usage as possible.

Key Interview Distinction: const Does Not Mean Immutable const prevents reassignment of the variable binding, not mutation of the value. const obj = {}; obj.key = 'value'; works fine — you're mutating the object, not reassigning obj. This is the most commonly misunderstood aspect and a frequent interview trick question.

Fun Fact

Brendan Eich created JavaScript in 10 days in May 1995. The var keyword was the only way to declare variables for 20 years until ES6 added let and const in 2015. The name 'JavaScript' was a marketing decision by Netscape to ride Java's popularity — Eich originally called it 'Mocha,' then 'LiveScript,' before management insisted on 'JavaScript.'

Continue Learning

Hoisting

beginner

Practice What You Learned

What is the difference between var, let, and const in JavaScript?
junior
variables
var is function-scoped and can be redeclared, let is block-scoped and can be reassigned, const is block-scoped and cannot be reassigned after initialization.
Previous
Type Coercion
Next
Scope & Scope Chain
PrevNext