Difference between var, let, and const in JavaScript

  • In JavaScript, var, let, and const are used to declare variables, but they have some important differences:
  • var is function-scoped, while let and const are block-scoped.
  • This means that variables declared with var are accessible throughout the entire function they are declared in, while variables declared with let or const are only accessible within the block they are declared in. A block is defined by a set of curly braces {}. For example:

    function exampleFunction() {
      var x = 1;
      let y = 2;
      const z = 3;
      if (true) {
        var x = 4;
        let y = 5;
        const z = 6;
        console.log(x, y, z); // 4 5 6
      }
      console.log(x, y, z); // 4 2 3
    }
    exampleFunction();

  • In this example, the var variable x is accessible both inside and outside the if block, while the let and const variables y and z are only accessible within their respective blocks.
  • Why var is function scope:

    function exampleFunction() {
        var x = 1;
        console.log("inside function x =", x)
    }

    exampleFunction();

    try {
        console.log("outside function x =", x)
    } catch (e) {
        console.log("x variable isn't access outside of function")
    }

    // =====================================================

    if (true) {
        var y = 1;
        console.log("inside if block y =", y)
    }
    try {
        console.log("outside if block y =", y)
    } catch (e) {
        console.log("y variable isn't access outside of if block")
    }

  • Output:
inside function x = 1
x variable isn't access outside of function
inside if block y = 1
outside if block y = 1
  • In the example above, even though the variable y is declared inside the if block, it is accessible outside the block because of the function-level scoping of var. This behavior is often considered a source of confusion and bugs in JavaScript.
  • To overcome this scoping issue, the let and const keywords were introduced in ECMAScript 2015 (ES6). Unlike var, variables declared with let and const have block-level scope, which means they are limited to the block where they are defined. This provides more predictable and intuitive scoping behavior in JavaScript.
  • var allows redeclaration, while let and const do not. this means that you can declare the same variable multiple times using var, and it will not produce an error. However, if you try to redeclare a variable using let or const, it will produce an error. For example:

    var x = 1;
    var x = 2; // No error
    let y = 3;
    let y = 4; // Error: Identifier 'y' has already been declared
    const z = 5;
    const z = 6; // Error: Identifier 'z' has already been declared

  • let and const are not hoisted, while var is hoisted.
  • Hoisting is a mechanism in JavaScript where variable and function declarations are moved to the top of their respective scopes. This means that you can use a variable before it has been declared, and it will not produce an error. However, if you try to access a let or const variable before it has been declared, it will produce an error. For example:

    console.log(x); // undefined
    var x = 1;

    console.log(y); // ReferenceError: y is not defined
    let y = 2;

    console.log(z); // ReferenceError: z is not defined
    const z = 3;

  • const is a read-only variable, while let and var can be reassigned. this means that once you assign a value to a const variable, you cannot reassign it to a new value. However, let and var variables can be reassigned to new values. For example:

    const x = 1;
    x = 2; // Error: Assignment to constant variable.

    let y = 3;
    y = 4; // No error.

    var z = 5;
    z = 6; // No error.

  • In summary, var, let, and const are used to declare variables in JavaScript, but they have some important differences in terms of scope, hoisting, redeclaration, and mutability. It's important to understand these differences in order to write effective and bug-free JavaScript code.

No comments:

Post a Comment