Difference between var and let in JavaScript

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

I am a Front end developer currently working on React JS, HTML, CSS, Python, Vue JS, Webpack, Babel, AEM etc.
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!
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.
Kritika Pattalam Bharathkumar Thanks! 😊
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!
Glad you found it useful. :)
This series talks about Tips & Tricks and simple projects you can build using JavaScript. Click on the articles below to read more.
Let's see 3 different ways to remove an element at the end of the array
Are you planning to start your coding journey?. Then this is for you - a roadmap to help you achieve your goals.

7 ways in which you can add files to your commit using git add

As a website user, one would have come across links or buttons which say "Click to download" or "Download" on a website. Have you ever wondered how can you add one to your website?. Keep reading!!!. As part of this blog let us see how one can crea...

Display Hello World and custom message using two-way data binding

Build a random quote generator using an API - JavaScript

Kritika Pattalam Bharathkumar | Web Developer - The official blog of Kritika Pattalam Bharathkumar
22 posts
Front End Engineer | Writes about HTML, CSS, JavaScript and everything related to web development | Follow me on Twitter let's chat.
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
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.
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
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
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