1. javascript
  2. /es6
  3. /let-and-const

Let and Const as JavaScript ES6 Features

Introduction

JavaScript is known for its versatile nature and the ability to adapt and find ways to handle complex web development tasks. With the arrival of ES6, the language has changed for the better.

Two of the most essential features of ES6 JavaScript are let and const, which have completely redefined the way we declare variables in JavaScript. These two features have brought a new level of control over the scope of variables and how they can be manipulated.

The Subtle Differences

Before ES6, var was the only way to declare variables in JavaScript. However, with the introduction of let and const, the process of declaring and initializing variables has become more nuanced. Unlike var, both let and const are block-scoped, meaning that they are only accessible within the block in which they are declared. In contrast, var is function-scoped, which means it can be accessed within the entire function.

One key difference between let and const is that let allows for reassigning, while const does not. Logically, this means that once we declare a variable using const, its value cannot be changed. On the other hand, a let variable can be reassigned as many times as necessary.

The Nature of let and const

The behavior can be somewhat confusing, especially for those who are used to working with var. For starters, let and const do not become properties of the window object, which is different from var. Additionally, they are not hoisted, meaning we only gain access to them within their declared block and we can't use them before they are declared.

Block Scoping

One concept that differentiates let and var is the concept of block scoping. As mentioned earlier, let and const are block-scoped, while var is function-scoped. So, if a variable is declared using let or const within a block, it can only be accessed within that block.

if (true) {
  let x = 10;
}
console.log(x); // ReferenceError: x is not defined

In this example, the x variable is declared within the if block, and it can only be accessed within it. If we try to access it outside of the block, we get a ReferenceError.

Reassigning and Redeclaring

Let's take a look at the differences in how variables behave when we reassign or redeclare them. With var, you can reassign a variable to a new value at any time, and even redeclare the same variable within the same scope.

var x = 10;
x = 20;
var x = 30;
console.log(x); // 30

With let and const, you can reassign a variable to a new value, but you cannot redeclare a variable within the same scope.

let x = 10;
x = 20;
let x = 30; // Uncaught SyntaxError: Identifier 'x' has already been declared
const y = 10;
x = 20; // Uncaught TypeError: Assignment to constant variable.
const x = 30; // Uncaught SyntaxError: Identifier 'x' has already been declared

So, the general rule of thumb is to use const when we don't need to reassign the variable and use let when we need to reassign.

Variety of Variable Examples

We can encounter numerous use cases for variables in web development. Let's take a glance at a few common encounters:

Loops

One of the most common uses for variables in web development is for looping. We can use let to declare a variable for each iteration of the loop, and this variable will only be available within the scope of the loop.

for (let i = 0; i < 10; i++) {
  console.log(i); // Iterates from 0 to 9 and outputs 0, 1, 2 ... 9
}
console.log(i); // Uncaught ReferenceError: i is not defined

Storing Data

You can also use variables to store data, such as user input or API responses. With const, we can ensure that the data is never reassigned, which can help prevent bugs and make your code more predictable.

const userInput = prompt("Enter your name:"); // We enter the name Brendan in the pop-up window
console.log(`Hello, ${userInput}!`); // Output: Hello, Brendan!
userInput = "Bob"; // Uncaught TypeError: Assignment to constant variable.

Final Thoughts

These essential features of ES6 JavaScript gave developers more control over their variables. They provided a way to declare them with stricter rules around reassigning and redeclaring, which helped prevent bugs and made code bases less prone to errors in the first place.

Additional Resources

Refresh your memory of variables with our Introduction to Variables in JavaScript

We recommend playing around with some engaging code snippets using variables to solidify your newly acquired knowledge.