DEV Community

Cover image for var, let, const: what's the difference?
Romain Trotard
Romain Trotard

Posted on • Edited on • Originally published at romaintrotard.com

var, let, const: what's the difference?

Today we are going to talk about an important subject to know as a javascript developer.Everyday you probably use var, let or/and const, but do you know the difference between them. It's what we are going to talk about.

var

The var statement is less and less used nowadays, but it doesn't mean that it's bad to use it.
In fact there are some tricky behaviors that can makes the code less predictive.

Reassignment

A variable declared with var can be reassigned with another value:

var myVariable = "First value";

myVariable = "Second value";
Enter fullscreen mode Exit fullscreen mode

*Note: You are not forced to initialized it:

var notInitializedVar;
Enter fullscreen mode Exit fullscreen mode

Declaration of var with same name

You can declare multiple variable with the same name (or label):

var redeclaredVar = 'First declaration';

var redeclaredVar = 'Second declaration';
Enter fullscreen mode Exit fullscreen mode

Scope of var

The scope of var depends on where it's declared.

In a function

When declared inside a function, the scope will be the entire function. Yep event if you declared it inside a if, it will be accessible outside of it:

function myDummyFunction() {
  if (true) {
    var myVariable = "A variable declared with var";
  }

  console.log(myVariable);
}

// Will print "A variable declared with var"
myDummyFunction();
Enter fullscreen mode Exit fullscreen mode

Note: The variable is also accessible in inside "blocks" (for example if block or inner function):

function myDummyFunction() {
  var myVariable = "A variable declared with var";

  if (true) {
    console.log(myVariable);
  }
}

// Will print "A variable declared with var"
myDummyFunction();
Enter fullscreen mode Exit fullscreen mode

Outside of a function

When we declared a variable with var outside of a function, the variables will be a globally-scoped variable.

For example if you type in the dev tool of your browser:

var myGlobalVariable =
  "Hello, I will be accessible from the window";

// Will print "Hello, I will be accessible from the window"
console.log(window.myGlobalVariable);
Enter fullscreen mode Exit fullscreen mode

Warning: When working with ES modules and commonJS modules, this variable will only be scoped to the module.

Note: That's why Immediately Invoked Function Expression (IIFE) is born, to prevent variable to be in the global scope and conflict with ones of other libraries.

Hoisting

Firstly, let's define what is hoisting: it's the behavior to put variable or function at the top of the file automatically.
That's thanks to the hoisting you can declare your function after using it:

hoistedFunction();

function hoistedFunction() {
  console.log("I can be called before my declaration");
}
Enter fullscreen mode Exit fullscreen mode

The particularity with var, it's that the variable is hoisted at the top of the file or of the function (if declared inside a function) and that it's initialized to undefined.

// Will print "undefined"
console.log(myHoistedVar);

var myHoistedVar = "I am a hoisted and initialized var";
Enter fullscreen mode Exit fullscreen mode
function myDummyFunction() {
  // Look the variable is declared inside the if
  // and the condition is always false
  console.log(myVariable);

  if (false) {
    var myVariable = "A variable declared with var";
  }
}

// Will print "undefined"
myDummyFunction();
Enter fullscreen mode Exit fullscreen mode

Unqualified identifier assignment

In non strict mode (without use strict;), if you declared a variable with only its label and no qualifier (var, let or const), this one will be automatically assigned to var:

unqualifiedVar =
  "I will be automatically be qualified \
with var in non strict mode";

// This will be transformed into
var unqualifiedVar =
  "I will be automatically be qualified \
with var in non strict mode";
Enter fullscreen mode Exit fullscreen mode

Warning: In strict mode it will not work, and will throw you a ReferenceError.

"use strict";

// You will see in your console something like
// Uncaught ReferenceError: unqualifiedVar is not defined
unqualifiedVar = "Will throw a ReferenceError";
Enter fullscreen mode Exit fullscreen mode

let

In everyday life, you probably use more let variable than var. But let's refresh our knowledge about it:

Reassignment

Like var, you can reassign a variable declared with let:

let myVariable = "First value";

myVariable = "Second value";
Enter fullscreen mode Exit fullscreen mode

Scope of let

Here is one of the main difference with var. A variable qualified with let will be block scoped (ie only be accessible inside the current closer parent curly bracket).

function myDummyFunction() {
  let myVariable = "A let variable";

  if (true) {
    console.log(myVariable);
  }
}

// Will print "A let variable"
myDummyFunction();
Enter fullscreen mode Exit fullscreen mode

Unlike var it will throw a ReferenceError if you try to access to a variable declared in a inner block:

function myDummyFunction() {
  if (true) {
    let myVariable = "A let variable";
  }

  console.log(myVariable);
}

// You will see in your console something like
// Uncaught ReferenceError: myVariable is not defined
myDummyFunction();
Enter fullscreen mode Exit fullscreen mode

Note: A variable declared with let will never be globally-scoped.

Cannot redefined a same label

Unlike var you cannot defined another variable with the same label (identifier). Otherwise you will see a SyntaxError in your console.

let myLetVariable = "First value";

// You will see in your console something like
// Uncaught SyntaxError: Identifier 'myLetVariable' has
// already been declared
let myLetVariable = "Second value";
Enter fullscreen mode Exit fullscreen mode

What about hoisting

Despite what you may see on the web let (like const) are hoisted but the difference with var is that they are not initialized to undefined.
Until the variable is not initialized you will have a ReferenceError if you try to access it.

console.log(myLetVariable);

// You will see in your console something like
// Uncaught ReferenceError: myLetVariable is not defined
let myLetVariable = "Some value";

// From here no more TDZ
Enter fullscreen mode Exit fullscreen mode

This behavior is called Temporal Dead Zone.

If you are asking why is the term temporal being used?
In fact it's because it depends of when the code is executed. For example if you have the following code, it's totally fine:

setTimeout(() => console.log(myLetVariable)), 500;

let myLetVariable = "Some value";

// After 500 ms you will see
// "Some value" prints in the console
Enter fullscreen mode Exit fullscreen mode

const

A variable declared with const has very similar properties than let. The only difference is about reassignment an initialization.

No reasignment

With a variable declared with const, it's not possible to reassign this variable with another value:

const myConstVariable = "First value";

// You will see in your console something like
// Uncaught TypeError: Assignment to constant variable
myConstVariable = "Second value";
Enter fullscreen mode Exit fullscreen mode

Warning: A variable declared with const is mutable:

const person = {};

// Mutation is done here
person.firstName = "Romain";
Enter fullscreen mode Exit fullscreen mode

Initialization

You have to initialized a variable qualified with const, otherwise you will have a SyntaxError.

// You will see in your console something like
// Uncaught SyntaxError: Missing initializer in const declaration
const uninitializedConst;
Enter fullscreen mode Exit fullscreen mode

Conclusion

I hope things are clearer in your mind. If you have to remember something, I guess it will be than scope between var and let/const is different.
var has function scope when declared inside a function and global scope when outside. let and const has block scope.
let variable can be reassigned contrary to const. But watch out, a variable declared with const is not immutable.
All three are hoisted but var is initialized to undefined unlike let and const that are not.

Here is a little table to resume:

Reassign Redeclare Scope Hoisted Unqualified variable
var ✔️ ✔️ function or global ✔️ (initialized to undefined) true
let ✔️ ✖️ block ✔️ (not initialized, TDZ) ✖️
const ✖️ ✖️ block ✔️ (not initialized, TDZ) ✖️

Do not hesitate to comment and if you want to see more, you can follow me on Twitter or go to my Website. 🐼

Top comments (2)

Collapse
 
zyabxwcd profile image
Akash

nice. I faintly remembered that initialising thing with 'const'. The thing that you mentioned about temporal dead zone was really amusing. The fact that if you put it inside an async function like setTimeout, it will not result in an error.

Collapse
 
romaintrotard profile image
Romain Trotard

Thanks :)
Yep it's not straightforward to have it, it's really needed to think in a temporal way