Search⌘ K

Tip 1: Signal Unchanging Values With const

Explore how to effectively use const in JavaScript to declare variables that should not be reassigned. Understand the difference between immutability and reassignment restriction, and learn how const simplifies code reading and maintenance by signaling stable values.

Which variable declaration to use?

Modern JavaScript introduced several new variable declarations, which is great. But it also introduced a new problem: Which variable declaration should be the default? And when should we use another type?

In the past, you had only one option for non-global variable assignment: var. Now there are many different options—var, let, and const—and each one has an appropriate usage. Try and keep things simple. In most cases, const is the best choice, not because it allows you to do the most, but because it lets you do the least. It has restrictions that make your code more readable.

ECMAScript is the official technical specification for JavaScript. JavaScript incorporated major syntax changes in ECMAScript 5 and ECMAScript 6, which are referred to as ES5 and ES6. Going forward, the spec will be updated yearly. Most developers now refer to the spec by year, such as ES2017.

Using const

const is a variable declaration that you ...