Understanding the Differences: var vs let keyword in JavaScript

Subham SinghSubham Singh
3 min read

Like every other programming language variable declaration is vital for storing and handling data, it enables the dynamic behavior of your programs. They help manage scope, improve memory efficiency, and play a crucial role in creating flexible and adaptable code.

So in JavaScript, we declare variables using the let, var and const, but we will be talking about let and var only.

let temp = "Hello World"; // initialising temp using let
var temp2 = "How are you"; // initialising temp2 using var

Scopes and potential issues with var

  • Function Scope

    Variables declared using the var keyword exhibit function scope, implying that a variable declared within a function will only be accessible within that function. Conversely, if declared outside any function, the variable's accessibility will extend throughout the entire program.

      var a = 10;
      function test(){
          var b = 5;
          console.log(a); // 10
          console.log(b); // 5
      }
      console.log(a);  // Error
      test();
    
  • Block Scope

    var does not have a block scope as we have in let and const, which means a variable declared inside a block can be accessed outside the block.

      if(true){
          var a = 10;
      }
      console.log(a); // 10
    

    Using var in such conditions is not recommended in modern JavaScript.

  • Variable Hoisting

    A variable declared using var is hoisted at the top of its function or the global scope, which means that the variable is effectively moved to the top of its scope during compilation before the code is executed. However, only the declaration is moved, not the initialization.

    And this can lead to unexpected behavior in the program.

    For example:

      var a = 0;
    
      function test() {
          if (true) {
              var a = 10; // This creates a new local variable, shadowing the global value of `a`            
          }
          console.log(a); // Output: 10
      }
    
      test();
      console.log(a); // Output: 0
    

The importance of using let in JavaScript

Using let is recommended in modern JavaScript development because of its block-scoping behavior, which brings several benefits.

  • Avoiding Global Scope Issues

    One of the primary pitfalls of var is its tendency to create variables in the global scope when not properly managed. This can lead to bugs that are hard to track down. let mitigates this risk by keeping variables within the scope they're intended for.

  • Code Clarity

    Since it is block scoped, it makes the code easier to understand. When you see a let declaration within a specific block, you immediately know where the variable is valid and how it's intended to be used.

  • Refactoring and Maintenance

    You can change or remove variables within a specific block without affecting other parts of the code, reducing the likelihood of unintended side effects.

  • Minimized Hoisting Issues

    While var declarations are hoisted to the top of their scope, sometimes leading to unexpected behavior, let declarations are hoisted but remain uninitialized until the actual declaration point. This reduces the risk of code behaving differently than expected due to hoisting.

Summary

Although var once had its uses, its global scope and hoisting problems make it less fitting for modern JavaScript. Opting for let introduces block scope, enhancing code clarity and predictability. Giving precedence to let in modern JavaScript guarantees well-scoped, maintainable, and dependable code, aligning seamlessly with present coding standards.

0
Subscribe to my newsletter

Read articles from Subham Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Subham Singh
Subham Singh

Hi, I'm Subham, a software engineer, and tech enthusiast. Welcome to my developer blog. I will be talking about the fascinating world of programming and all things tech. Let's learn and grow together in this ever-evolving landscape!