Skip to main content

Command Palette

Search for a command to run...

Difference between var and let in JavaScript

Published
2 min read
Difference between var and let in JavaScript
K

I am a Front end developer currently working on React JS, HTML, CSS, Python, Vue JS, Webpack, Babel, AEM etc.

As a JavaScript developer, we have been told multiple times that var is similar to let, and as part of ES6, we should all move to use let instead of var.

But do we really know what is the difference between them both and why should one consider using let?. Let's find out.

Here are a few of the things which differentiate var from let

  1. Global Object
  2. Re-declaration
  3. Hoisting
  4. Scoping

1. Creating global object

When using var at the top of the file, it creates a **global property object which is windows, which does not apply when using let instead.

var counter = 0;
let counter2 = 0;
console.log(window.counter); //  0
console.log(window.counter2); //  undefined

In the above example, both counter and counter2 are global objects created at the top, but when trying to access as via window.variableName let results in undefined error.

2. Re-declaration

In strict mode, let cannot be redeclared and if done it will throw an error, whereas var can be redeclared.

'use strict';
var string1= "hello";
var string1= "hi"; // "hello" will be replaced without any errors

let string2= "world"; 
let string2= "hello"; // SyntaxError: Identifier 'string2' has already been declared

3. Hoisting

Hoisting is when variables can be used even before they are declared. In the below example the variable string1is used, here it saves space for foo with the value undefined and the declaration/initialization follows later in the code. But whereas with let, this is not possible it will hit a temporal dead zone resulting in a reference error

console.log(string1); // undefined
var string1= "hello";
console.log(string1); // hello

console.log(string2); // ReferenceError
let string2= "world";
console.log(string2); // world

4. Scoping

The main difference between let and var is scoping of the variables. In the below example, Var string3 inside Level-enclosed brackets are available even outside the level when inside a function, whereas let is block-level scoped or enclosing brackets scoped.

function run() {
  var string1= "Hello1";
  let string2= "World1";

  console.log(string1, string2); // Hello1 World1
 //Level - enclosed brackets
  {
    var string3= "Hello2"
    let string4= "World2";
    console.log(string3, string4); // Hello2 World2
  }

  console.log(string3); // Hello2
  console.log(string4); // ReferenceError
}

In the above example, since let is scoped to block or enclosing brackets, when you try accessing string4 outside of the braces block it will not be available and throw an reference error.

Follow me on Twitter | LinkedIn for more web development related tips and posts. Feedbacks and suggestions are welcome.

Checkout my other blog post series

L

Thank you for writing great post! 😄 I've seen difference between const and let post many time, but rarely find that mentioning about var certainly.

I've interests your other article!

1
K

Very true, even I hardly find articles explaining let vs var, that is why I wanted to write this. Thanks for taking the time to read.

1
T

Nice introduction. Thanks for sharing.

1
K

Thanks for taking the time to read. Its really awesome to get feedback from you!!!.

A

Nice article! I too published an article on var let and const in JS yesterday, though I missed the strict mode. This is an addition to my knowledge on JS. Thanks for publishing the article!

1
K

Glad you found it useful. :)

JavaScript

Part 7 of 10

This series talks about Tips & Tricks and simple projects you can build using JavaScript. Click on the articles below to read more.

Up next

Different ways to remove an element at the end of the array using JavaScript

Let's see 3 different ways to remove an element at the end of the array