Learn ES6 (3/31) | Declare a Read Only Variable with the const Keyword | freeCodeCamp

Published: 29 April 2021
on channel: Stral Tech
652
3

Learn ES6 (3/31) | Declare a Read Only Variable with the const Keyword | freeCodeCamp

The keyword let is not the only new way to declare variables. In ES6, you can also declare variables using the const keyword.

const has all the awesome features that let has, with the added bonus that variables declared using const are read-only. They are a constant value, which means that once a variable is assigned with const, it cannot be reassigned.

const FAV_PET = "Cats";
FAV_PET = "Dogs";
The console will display an error due to reassigning the value of FAV_PET.

As you can see, trying to reassign a variable declared with const will throw an error. You should always name variables you don't want to reassign using the const keyword. This helps when you accidentally attempt to reassign a variable that is meant to stay constant. A common practice when naming constants is to use all uppercase letters, with words separated by an underscore.

Note: It is common for developers to use uppercase variable identifiers for immutable values and lowercase or camelCase for mutable values (objects and arrays). In a later challenge you will see an example of a lowercase variable identifier being used for an array.

Change the code so that all variables are declared using let or const. Use let when you want the variable to change, and const when you want the variable to remain constant. Also, rename variables declared with const to conform to common practices, meaning constants should be in all caps.


Watch video Learn ES6 (3/31) | Declare a Read Only Variable with the const Keyword | freeCodeCamp online, duration hours minute second in high quality that is uploaded to the channel Stral Tech 29 April 2021. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 652 times and liked it 3 visitors.