Create a Variable: const

The const keyword was also introduced in ES6, and is short for the word constant. Just like with var and let you can store any value in a const variable. The way you declare a const variable and assign a value to it follows the same structure as let and var. Take a look at the following example:

const myName = 'Gilberto'; console.log(myName); // Output: Gilberto

However, a const variable cannot be reassigned because it is constant. If you try to reassign a const variable, you’ll get a TypeError.

Constant variables must be assigned a value when declared. If you try to declare a const variable without a value, you’ll get a SyntaxError.

If you’re trying to decide between which keyword to use, let or const, think about whether you’ll need to reassign the variable later on. If you do need to reassign the variable use let, otherwise, use const.



Create a constant variable named entree and set it to equal to the string 'Enchiladas'.


Just to check that you’ve saved the value of 'Enchiladas' to entree, log the value of entree to the console.


Great, let’s see what happens if you try to reassign a constant variable.

Paste the following code to the bottom of your program.

entree = 'Tacos'

This code throws the following error when you run your code:

TypeError: Assignment to constant variable.

After you clear this checkpoint, if you want to see about another quirk of const in action open the hint!

Folder Icon

Take this course for free

Already have an account?