Getting Started
Level 1
- 4 - Comments
- 5 - Displaying Text
- 6 - Variables - Let
- 7 - Variables - Constants
- 8 - Arithmetic Operators
- 9 - Functions
- 10 - If/Else Statements
- 11 - Comparison Operators
- 12 - If/Else Loops
- 13 - Boolean Operators
- 14 - Level 1 Complete!
Level 2
Level 3
Useful Links
Step 7 - Variables - Constants
A constant is just like a variable, but it must be given a value when it is declared (created), and that value can never change. In other words, a constant always contains the same information.
To declare (create) a constant, we use the keyword const
.
const earthRadiusKm = 6371;
Assigning a new value to the constant will cause an error, we can not do:
earthRadiusKm = 6400;
The name of constants can also be used to represent the information they contain, in the same way as variables.
Tip:
Don’t forget to comment out alerts if you don’t want them to pop-up every time.
Challenge:
Create a constant, and display it an alert pop-up box.
Answer
const dogLegs = 4;
alert(dogLegs)
Challenge:
Try to assign a new value to your constant to see what happens.
Answer
const dogLegs = 4;
dogLegs = 3;
alert(dogLegs)