The difference between let, const, and var

The difference between let, const, and var

JavaScript Variables

JavaScript Variables are among the most fundamental concept in JavaScript, as well as other programming languages. These variables are used to store data temporarily in the computer's memory. In the early days of JavaScript, variables could only be declared using the var keyword. However, variables declared with var couldn't meet specific needs in programming. Now, there are three (3) ways you can declare a variable in JavaScript:

  1. Var
  2. Let
  3. Const

Var and Let are really similar. However, the difference between them is that Var is globally scoped while Let is not.

Var

Var declarations are globally scoped or function/locally scoped. Also, Var variables can be re-declared and updated.

When is var global scoped?

The scope is global when a var variable is declared outside a function. This means that any variable declared with var outside a function block is available for use in the whole window.

When is var "function scoped"?

Var is function scoped when it is declared within a function. This means that it is available and can be accessed only within that function.

Let

Let is preferred for variable declarations. This is because it comes as an improvement to var declarations. Just like var, a variable declared with let can be updated within its scope. But unlike var, a let variable cannot be re-declared within its scope.

One of the major advantages of let is that when it's used, you don't have to bother if you have used a name for a variable before. This is because a variable exists only within its scope.

Again, let declarations are hoisted to the top like var. But the let keyword is not initialized, unlike var which is initialized as undefined.

For instance

let greeting = "say Good day";
    if (true) {
        let greeting = "say Welcome instead";
        console.log(greeting); 
// "say Welcome instead"
    }
    console.log(greeting); 
// "say Good day"

The above code will be error-free because they are treated as different variables since they have different scopes. This fact makes let better than var.

Const

Variables declared with the const usually maintain constant values. const declarations are somehow similar to let declarations. Like let, const declarations can only be accessed within the block they were declared. However const can neither be updated nor re-declared. This means that const values remain the same within its scope. Thus, every const declaration must be initialized at the time of declaration.

And Just like let, const declarations are hoisted to the top but are not initialized.

What are the differences between Var, Let, and Const?

  • Var declarations are globally scoped or function scoped while let and const are block scoped.
  • Var variables are initialized with undefined, while let and const variables are not initialized.
  • Var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; while const variables can neither be updated nor re-declared.
  • Const must be initialized during declaration, unlike var and let.

I hope this article was insightful and answered your questions. Until next time, Thanks for reading!