Blog / September 21, 2023 / 4 mins read / By Suneet Agrawal

Let vs Var vs Const in JavaScript

JavaScript is a versatile and widely used programming language that offers multiple ways to declare variables, including let, var, and const. Each of these declarations serves a specific purpose and has its own scope and behavior. In this blog post, we’ll dive into the differences between let, var, and const in JavaScript with examples to help you understand when and how to use them effectively.

var - The Traditional Variable Declaration

var is the oldest way to declare variables in JavaScript. Variables declared with var are function-scoped, which means their scope is limited to the function they are declared in or the global scope if declared outside any function.

Example:

function exampleVar() {
  var x = 10;
  if (true) {
    var x = 20; // This reassigns the outer 'x'
  }
  console.log(x); // Outputs 20
}

In the above example, the if block doesn’t create a new scope for x, so it reassigns the outer x to 20.

let - Block-Scoped Variables

let was introduced in ECMAScript 6 (ES6) to address some of the issues with var. Variables declared with let are block-scoped, meaning they are confined to the block (enclosed by curly braces) in which they are defined.

Example:

function exampleLet() {
  let x = 10;
  if (true) {
    let x = 20; // This is a separate 'x' from the outer one
  }
  console.log(x); // Outputs 10
}

Here, the inner x is distinct from the outer x, thanks to let’s block-level scope.

const - Constants

const is another ES6 addition that allows you to declare variables that cannot be reassigned after their initial value is set. Like let, const is block-scoped.

Example:

function exampleConst() {
  const x = 10;
  if (true) {
    const x = 20; // This is a separate 'x' from the outer one
  }
  console.log(x); // Outputs 10
}

In this case, you can’t reassign the x variable inside the if block because it was declared as a constant.

let vs var vs const : Key Differences

Scoping:
  • var: Function-scoped or globally scoped.
  • let: Block-scoped (limited to the block or function they are defined in).
  • const: Block-scoped (like let) and constants (cannot be reassigned).
Reassignment:
  • var: Allows reassignment within its scope.
  • let: Allows reassignment within its scope.
  • const: Does not allow reassignment once defined.
Hoisting:
  • var: Hoisted to the top of its function or global context. It can be used before it`s declared (but will be undefined).
  • let: Hoisted to the top of its block or function, but not initialized. It results in a ReferenceError if used before declaration.
  • const: Hoisted like let, but also results in a ReferenceError if used before declaration.
Usage for Constants:
  • var: Not suitable for declaring constants as it allows reassignment.
  • let: Not suitable for declaring constants as it allows reassignment.
  • const: Ideal for declaring constants that should not be changed.
Global Object Property:
  • var: Creates a property on the global object (e.g., window in a browser).
  • let: Does not create a global object property.
  • const: Does not create a global object property.
Temporal Dead Zone (TDZ):
  • var: No TDZ; variables are initialized with undefined.
  • let: Has a TDZ; accessing a let variable before declaration results in a ReferenceError.
  • const: Also has a TDZ; accessing a const variable before declaration results in a ReferenceError.
Use Cases:
  • var: Used less frequently in modern JavaScript due to its unpredictable scoping behavior. It`s mainly used in legacy codebases.
  • let: Preferred for most variable declarations, especially when reassignment may be required within a block.
  • const: Preferred for declaring constants and values that should remain unchanged.

When to Use Each Declaration:

  • Use var sparingly, as it has a global or function scope, which can lead to unintended variable hoisting and potential issues in your code. In modern JavaScript, let and const are often preferred over var.
  • Use let when you need a variable that can be reassigned within the same block or function. It provides more predictable scoping behavior than var.
  • Use const when you want to declare a constant value that should not be reassigned. It’s a good practice to use const for values that should remain unchanged throughout your code.

Conclusion:

Understanding the differences between let, var, and const in JavaScript is crucial for writing clean and maintainable code. By choosing the appropriate declaration based on your needs, you can avoid scope-related bugs and make your code more robust. As JavaScript continues to evolve, let and const have become the preferred choices for variable declarations, with var being used less frequently in modern codebases.

Comments