Var, Let, and Const in JavaScript Explained

Last Updated: 25 Feb, 2023

What are var, let, and const keywords in JavaScript?

In JavaScript, var, let, and const are three different ways of declaring different types of variables. Previously there was only one way of declaring JavaScript variables using var keyword. In ES6, let and const keywords have been introduced that are used to declare block scoped variables. Currently most of the time we use let and const to declare variables.

Var - Declares current-scoped variables

  • All varibales declared using var keyword have the current execution context scope.
  • If a varibale is decalred with var keyword inside a function scope, it can only be accessed inside that function.
  • If the variable is declared outside the function, you can access it inside the function but if the variable is declared inside the function, you cannot access it from outside the function.

Example:

try{
    var no_of_months_in_year = 12;

    (function () {
        var no_of_days_in_week = 7;
        console.log("===== Inside Function =====");
        console.log("Number of days in a week is: " + no_of_days_in_week); //No Error
        console.log("Number of months in a year is: " + no_of_months_in_year); //No Error
    })();

    console.log("===== Outside Function =====");
    console.log("Number of months in a year is: "+no_of_months_in_year); //No Error
    console.log("Number of days in a week is: "+no_of_days_in_week); //Error
}catch(e){
    console.log("Error: " + e);
}

Output:

===== Inside Function =====
Number of days in a week is: 7
Number of months in a year is: 12
===== Outside Function =====
Number of months in a year is: 12
Error: ReferenceError: no_of_days_in_week is not defined

Let - Declares block-scoped variables

  • All variables declared using let keyword have the scope of block statement, or expression on which it is used.
  • Let variabes must be declared before they are used.
  • Becuase of this reason, let declarations are commonly considered as non-hoisted.
  • Let variables can be updated but cannot be redeclared.

Example:

try{
    let monthName = "January";

    (function () {
        let dayName = "Sunday";
        console.log(monthName); //prints January
        console.log(dayName); //prints Sunday
    })();

    monthName = "Fabruary";
    console.log(monthName); //prints Fabruary

    let monthName = "March"; // Error
}catch(e){
    console.log("Error: " + e.message);
}

Const - Declares block-scoped constant variables

  • All variables declared using const keyword are constants and have block level scope.
  • Scope can be local or global to the block in which it is declared.
  • Variables defined using const keyword cannot be redeclared.
  • Variables defined using const keyword must be assigned.
  • Variables defined using const keyword cannot be reassigned because it creates a read-only reference to a value.
  • If you define an array or object using const keyword, its properties can be updated or deleted.

Example:

const number = 1234500;
console.log(number); //prints 1234500
try{
    number = 5432100;
}catch(err){
    console.log("Error: " + err.message);
    //Error: Assignment to constant variable.
}

 

Thank You, Please Share.

Recommended Posts

PHP Sessions Explained

PHP Sessions Explained

In this post you will be gaining valuable insights about the PHP sessions and it's working functionalities.

PHP Cookies Explained

PHP Cookies Explained

In this post you will be gaining valuable insights about the PHP Cookies and it's working functionalities.

Implement Singleton Design Pattern in PHP

Implement Singleton Design Pattern in PHP

This tutorial will explain you How to implement Singleton Design Pattern in PHP with the help of comprehensive examples.