Skip to content

JavaScript const vs let

  • by

In JavaScript, const and let are both used to declare variables, but they have some differences in terms of their behavior and usage.

const: The const keyword is used to declare a constant variable. Once a constant variable is assigned a value, its value cannot be changed or reassigned. It provides a way to define variables that are meant to remain constant throughout the program execution.

const PI = 3.14159;
PI = 3.14; // This will throw an error

Key points about const:

  • It must be initialized at the time of declaration.
  • It cannot be reassigned to a new value.
  • It has a block scope, just like let.
  • If a const variable holds a reference to an object or array, the properties or elements of the object/array can still be modified. The immutability applies only to the variable reference itself.

let: The let keyword is used to declare a block-scoped variable that can be reassigned. It allows you to define variables that are limited in scope to the block, statement, or expression in which they are used.

let count = 5;
count = 10; // This is allowed

Key points about let:

  • It doesn’t require initialization at the time of declaration.
  • It can be reassigned to a new value.
  • It has block scope, meaning it is accessible only within the block it is defined in.
  • Variables declared with let can be updated and redeclared within the same block.

Here’s a tabular format comparing const and let in JavaScript:

#constlet
Variable typeConstant variableReassignable variable
InitializationMust be initialized at declarationOptional initialization at declaration
ReassignmentNot allowedAllowed
ScopeBlock scopeBlock scope
RedeclarationNot allowedAllowed
UsageUse for variables that remain constantUse for variables that may change
ImmutabilityOnly variable reference is immutableVariable value can be modified

In general, it is recommended to use const for variables that are not intended to change and use let for variables that need to be reassigned. By using const where possible, you can enforce immutability and make your code more robust, easier to reason about, and less prone to bugs. However, when you know a variable will change, or you need block-scoping behavior, you should use let.

JavaScript const vs let example

Simple example code that demonstrates the difference between const and let in JavaScript:

// Example using const
const PI = 3.14159;
console.log(PI); // Output: 3.14159

// Attempting to reassign a constant variable
PI = 3.14; // Throws an error: TypeError: Assignment to constant variable.

// Example using let
let count = 5;
console.log(count); // Output: 5

// Reassigning the let variable
count = 10;
console.log(count); // Output: 10

Output:

JavaScript const vs let

In this example, we first declare a constant variable PI and assign it the value of 3.14159. When we try to reassign a new value to PI, it throws a TypeError because we are not allowed to change the value of a constant variable.

Next, we declare a variable count using let and assign it the initial value of 5. We can then reassign a new value to count, in this case, 10, without any errors.

Comment if you have any doubts or suggestions on this Js difference topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading