Constants
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 11 of 77.
In JavaScript, sometimes you need variables that never change. This is where const comes in! It’s used to create constants—values that stay the same after being set.
Use the const keyword, just like let, but once you set its value, you cannot change it later.
const pi = 3.14;
console.log(pi); // Output: 3.14
// Trying to change pi will cause an error:
pi = 3.14159; // Error: Assignment to constant variable.Use const for values that should never change, like:
- Mathematical constants (
pi = 3.14) - Fixed settings (
maxUsers = 100)
By using const, your code is safer and easier to understand!
Challenge
BeginnerInitialize a constant variable named minSize with the value of 50
Cheat sheet
Use const to create variables that never change:
const pi = 3.14;
console.log(pi); // Output: 3.14Once set, const variables cannot be reassigned:
pi = 3.14159; // Error: Assignment to constant variable.Use const for:
- Mathematical constants (
pi = 3.14) - Fixed settings (
maxUsers = 100)
Try it yourself
// Type your code below
// Don't change the line below
console.log(`minSize = ${minSize}`)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False