const keyword was also introduced in ES6, and is short for the word constant. Just like with
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
var. Take a look at the following example:
const myName = 'Gilberto'; console.log(myName); // Output: Gilberto
const variable cannot be reassigned because it is constant. If you try to reassign a
const variable, you’ll get a
Constant variables must be assigned a value when declared. If you try to declare a
const variable without a value, you’ll get a
If you’re trying to decide between which keyword to use,
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
Create a constant variable named
entree and set it to equal to the string
Just to check that you’ve saved the value of
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!