Let and Const

Javascript 8 min min read Updated: Mar 07, 2026 Intermediate
Let and Const
Intermediate Topic 2 of 15

Let and Const in JavaScript

In modern JavaScript, variables can be declared using let and const. These keywords were introduced in ES6 (ECMAScript 2015) to provide better ways to manage variables and improve code reliability. Before ES6, developers mostly used the var keyword, which had several limitations related to scope and variable redeclaration.

The introduction of let and const solved many problems associated with var and made JavaScript code more predictable and easier to maintain. Understanding the difference between let and const is essential for writing modern JavaScript applications.

Why Let and Const Were Introduced

The var keyword allowed variables to be redeclared and had function-level scope, which often caused unexpected behavior in large programs. Let and const were introduced to give developers better control over variable scope and prevent accidental reassignment of important values.

  • They provide block-level scope
  • They prevent accidental variable redeclaration
  • They improve code readability
  • They help developers write safer JavaScript code

Understanding Let in JavaScript

The let keyword is used to declare variables that can change later. Unlike var, let follows block scope, meaning the variable is only accessible inside the block where it is declared.

javascript let name = "JavaScript"; name = "Node.js"; console.log(name);

In this example, the variable name is declared using let and later reassigned with a new value.

Block Scope with Let

Variables declared with let exist only inside the block where they are defined. A block can be a loop, condition, or any set of code enclosed in curly braces.

javascript { let message = "Hello Students"; console.log(message); } console.log(message);

The second console statement will cause an error because the variable message is only available inside the block.

Understanding Const in JavaScript

The const keyword is used to declare variables whose values should not change after initialization. Once a value is assigned to a const variable, it cannot be reassigned.

javascript const siteName = "Edugators"; console.log(siteName);

If you try to change the value of a const variable, JavaScript will throw an error.

javascript const pi = 3.14; pi = 3.14159;

This code will produce an error because const variables cannot be reassigned.

Const with Objects and Arrays

One important thing to understand is that const does not make objects or arrays completely immutable. It only prevents reassignment of the variable itself.

javascript const user = { name: "Rahul", age: 25 }; user.age = 26; console.log(user);

In this example, the object properties can still be modified even though the variable was declared using const.

Difference Between Let and Const

Both let and const follow block scope, but they behave differently when it comes to reassignment.

  • let allows reassignment of variables.
  • const does not allow reassignment.
  • Both follow block scope.
  • Both cannot be redeclared within the same scope.

Example Comparing Let and Const

javascript let score = 50; score = 70; const maxScore = 100; console.log(score); console.log(maxScore);

Here the variable score changes because it is declared using let, while maxScore remains constant.

Let and Const in Loops

Let is commonly used inside loops because it creates a new variable for each iteration.

javascript for (let i = 1; i <= 3; i++) { console.log(i); }

The variable i exists only within the loop block.

Best Practices for Using Let and Const

Modern JavaScript developers follow certain best practices when declaring variables.

  • Use const by default whenever possible
  • Use let when a variable needs to change
  • Avoid using var in modern JavaScript
  • Keep variable scope limited to improve readability

Real World Example

In real applications, developers often use const for values that should remain constant, such as configuration settings or API URLs.

javascript const apiURL = "https://api.example.com/users"; let userCount = 0; userCount++; console.log(apiURL); console.log(userCount);

Here the API URL remains constant while the user count can change.

Advantages of Let and Const

  • Better variable scoping
  • Prevents accidental redeclaration
  • Improves code readability
  • Encourages safer programming practices
  • Essential for modern JavaScript development

Conclusion

The introduction of let and const significantly improved how variables are handled in JavaScript. Let allows variables to be reassigned while maintaining block scope, whereas const ensures that values remain constant after initialization.

Using let and const properly helps developers write cleaner, more reliable, and more maintainable JavaScript code. These keywords are now widely used in modern JavaScript frameworks and applications.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators