W3docs

JavaScript Variables

The article from W3Docs on JavaScript variables covers the fundamentals of variable declaration and the usage of var, let, and const. It explains the scope and

Introduction

A variable is a named container for a value. In JavaScript you create variables to store data — a user's name, a running total, the result of a calculation — and then refer to that data by name later in your program.

This guide covers everything you need to declare variables confidently: the three keywords (let, const, and the older var), how scope decides where a variable is visible, what hoisting and the Temporal Dead Zone (TDZ) mean in practice, the difference between reassigning and mutating a const, and the naming rules and conventions the community follows.

Short version: use const by default, let when you need to reassign, and avoid var in modern code.

Declaring a Variable

You declare a variable with a keyword followed by a name, and optionally assign it a value with =:

javascript— editable

JavaScript is dynamically typed: a variable does not have a fixed type, so the same variable can hold a string now and a number later. (See JavaScript Data Types for the values variables can hold.)

There are three declaration keywords:

  • const — for values that won't be reassigned (your default choice).
  • let — for values that will be reassigned.
  • var — the original keyword, kept for backward compatibility. See The old "var".

let, const, and var Compared

KeywordScopeReassign?Redeclare in same scope?Hoisting
constblockNoNoTDZ
letblockYesNoTDZ
varfunctionYesYesHoisted, initialized to undefined

The example below shows the practical differences between block-scoped let/const and function-scoped var:

javascript— editable

Scope: Where a Variable Lives

Scope determines where in your code a variable can be accessed. For a deeper treatment, see Variable scope, closure.

Block Scope (let and const)

let and const are visible only inside the { ... } block where they're declared — an if, a loop, or any pair of braces:

javascript— editable

This is why let is the right choice for loop counters: each iteration of a for loop gets its own binding.

Function Scope (var)

var ignores blocks. It is scoped to the nearest enclosing function (or the global scope if outside any function), which is a common source of bugs:

javascript— editable

Global Scope

A variable declared outside any function is global and reachable everywhere. In browsers, a top-level var attaches to the global window object, while top-level let and const are script-scoped and do not:

javascript— editable

Hoisting and the Temporal Dead Zone

Hoisting means declarations are processed before the code runs. The three keywords differ in how they hoist.

A var declaration is hoisted and initialized to undefined, so reading it before the line that assigns it gives undefined rather than an error:

javascript— editable

let and const are also hoisted, but they are not initialized. From the start of the block until the declaration line, the variable sits in the Temporal Dead Zone (TDZ) — accessing it throws a ReferenceError:

javascript— editable

The TDZ is a feature, not a quirk: it catches accidental use-before-declaration that var silently hides.

const: Reassignment vs. Mutation

const prevents reassigning the variable — you cannot point the name at a new value. It does not make the value itself immutable. Objects and arrays held in a const can still be changed:

javascript— editable

Use const for objects and arrays whenever you don't reassign the binding — it documents intent and prevents an entire class of bugs. If you truly need an unchangeable object, use Object.freeze().

Naming Rules and Conventions

JavaScript enforces a few hard rules for variable names:

  • Names may contain letters, digits, _ (underscore), and $ (dollar sign).
  • Names must not start with a digit.
  • Names are case-sensitivetotal and Total are different variables.
  • Reserved keywords such as let, class, or return cannot be used.
  • Spaces are not allowed.

On top of the rules, follow these conventions for readable code:

  • Use descriptive names: userAge, not x.
  • Use camelCase for variables and functions: firstName, itemCount.
  • Use UPPER_SNAKE_CASE for fixed constants: const MAX_SIZE = 100;.
  • Avoid single letters except for short-lived loop counters (i, j).
javascript— editable

Summary

  • Use const by default, let when you need to reassign, and avoid var in new code.
  • let and const are block-scoped; var is function-scoped and leaks out of blocks.
  • let/const live in the TDZ before their declaration; var reads as undefined.
  • const blocks reassignment, not mutation — object and array contents can still change.
  • Follow naming rules (no leading digit, no keywords, case-sensitive) and use camelCase.

To go deeper, read The old "var" and Variable scope, closure.

Practice

Practice
Which of the following rules apply to creating JavaScript variables?
Which of the following rules apply to creating JavaScript variables?
Was this page helpful?