const

What Is const vs. var in JavaScript?

const and var both declare variables in JavaScript, but they differ in scope, hoisting and reassignment. Here is how each works and when to pick one.

On this page
  1. The short answer
  2. How var works
  3. How const works
  4. Side by side
  5. const stops reassignment, not changes to contents
  6. Coming from C++
  7. When var still shows up

The short answer

In JavaScript, const and var both declare variables, but they behave differently. const creates a name that can’t be reassigned, and it is block scoped. var is the older declaration. It is scoped to the enclosing function (or the global scope), and it allows the same name to be declared again. Most modern code uses const and let instead of var.

How var works

A var variable belongs to the function where it’s declared, or to the global scope if it sits outside any function. It ignores { } blocks:

function demo() {
  if (true) {
    var message = "hello";
  }
  console.log(message); // "hello": var ignores the block
}
demo();

var declarations are also hoisted. The declaration moves to the top of its scope and starts out as undefined, so reading it early doesn’t throw an error:

console.log(score); // undefined, not an error
var score = 10;

You can also declare the same var name twice in one scope, and JavaScript allows it without complaint:

var color = "red";
var color = "blue"; // allowed

How const works

const is block scoped. The variable exists only inside the nearest { } block:

if (true) {
  const greeting = "hi";
}
console.log(greeting); // ReferenceError: greeting is not defined

A const must get a value when it’s declared, and you can’t assign a new value to it afterward:

const rate = 0.05;
rate = 0.06; // TypeError: Assignment to constant variable.

Using a const name before its declaration also throws an error, unlike var. Declaring the same const name twice in one scope is a syntax error.

Side by side

var const
Scope Enclosing function, or global Enclosing block
Before the declaration Reads as undefined Throws a ReferenceError
Declared again in the same scope Allowed Not allowed
Reassigned later Allowed Not allowed
Needs a starting value No Yes

const stops reassignment, not changes to contents

This is the point that surprises most people coming from C++. const stops the name from pointing at a different value. It doesn’t freeze the value itself:

const items = ["pen", "cup"];
items.push("book"); // OK: the array's contents change
items = [];         // TypeError: Assignment to constant variable.

If you want an object to stay unchanged at the top level, Object.freeze can help. It only protects the object’s own properties, so nested objects can still change.

Coming from C++

In C++, a const object’s contents are protected through that name. JavaScript’s const works differently: it only fixes the binding. A rough comparison is a C++ int* const pointer. It can’t point somewhere else, but the value it points to can change. The c const keyword rules in C++ are covered in their own guide.

C++ does have auto, but it only deduces a type. It doesn’t change scope or reassignment, so it isn’t a stand-in for var or const.

When var still shows up

You’ll mostly see var in older code and in older tutorials. Replacing var with let or const can change behavior when the code depends on function scope or hoisting, so run the code and its tests after the change rather than assuming the result is the same.

The complete guide to const

The const Keyword in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Rust vs C++ Performance

Rust and C++ both compile to fast native code, so speed gaps are often small. Learn what drives performance in each, and how to read benchmarks fairly.

Rust vs C++ 6 min read