difference between Var, Let, and Const

·

3 min read

image.png the 6th version of ECMAScript was launched, a lot of shiny new features came out. And now, since it’s 2022, it’s assumed that most of the developers have become familiar with those features.

At the beginning of JavaScript, there is only one way to declare a variable and that is using the var keyword. With ES6, two new ways to define variables were introduced with let and const. Now, all the major browsers are compatible with the let and const keywords, and most of developers are using let and const nowadays.

let me simplify this complex language for you most of developers are using const nowadays for the following reasons "var" is not a block scope , It can be accessed anywhere in the code, regardless of where the block ends and if a variable is declared using "var" inside a function, it's scope is limited to the block of the function. let's see a example and increase our understanding over it

if(true){
var a = 4}

console.log(a)  //4, the variable lives after the block

Variables with same name within a scope can be declared using "var" any number of times. However, we are getting error if we use "let" .

var userName = "vivek";
var userName= "sunny";
console.log(userName) // we are getting sunny as output

"var" can be use as a variable even before its declaration using "var"

name = "vivek"
console.log(name);
var name ;

However, the above code will give error if we "let" instead of "var". When a programmer is sure that a variable will never change, he can make it a constant using the "const" keyword. Hence, it will give an error if someone tries to re-assign it a different value. Other than this very fact there is no difference between "const" and "let". lets see how let work

let

This is the improved version of var declarations. Variables declaration using this way eliminates all the issues that we discussed earlier. let creates variables that are block-scoped. Also, they can not be redeclared and can be updated. The below example shows it is the best choice to use let than var. let is a block scop

 let greeting = "hello";
   let times = 4;

   if (times > 3) {
        let hello = "say Hii instead";
        console.log(hello);// "say Hello instead"
    }
   console.log(hello) // hello is not defined

it will return not define This is because let variables are block scoped . let can be updated but not re-declared.

    let greeting = "hello";
    greeting = "good morning";

However, if the same variable is defined in different scopes, there will be no error:

  let greeting = "say Hi";
    if (true) {
        let greeting = "say Hello instead";
        console.log(greeting); // "say Hello instead"
    }
    console.log(greeting); // "say Hi"

Hoisting of let Just like var, let declarations are hoisted to the top. Unlike var which is initialized as undefined, the let keyword is not initialized. So if you try to use a let variable before declaration, you'll get a Reference Error. let us see how const works

Const

Variables declared with the const maintain constant values. const declarations share some similarities with let declarations. const can not re-declared and re-assign

  const Name = "vivek";
    const Name = "sunny";// error: Identifier 'Name' has already been declared
//while we cannot do this:

    greeting = {
        words: "Hello",
        number: "five"
    } // error:  Assignment to constant variable.
//we can do this:

    greeting.message = "say Hello instead";

Thank you for reading :)