Published
The const keyword in JavaScript is used to declare a constant, i.e., a variable that cannot be reassigned. Once a const variable is assigned, it cannot be changed. It must be initialized at the time of declaration. The purpose of const is to prevent modifications to a variable throughout the execution of code. It's important to consider using const when you want a variable to always have the same value.
In JavaScript, const is declared like any other variable, using the const keyword followed by the variable name. The variable should then be immediately initialized with a value, as const variables cannot be declared without being initialized.
const PI = 3.14159;
Even though a const variable can't be reassigned, it doesn't mean that it's immutable. If the const variable is an object or an array, the properties or elements can still be modified. Also, const declarations are block scoped, similar to variables declared with let, meaning they are only accessible within the block they are declared.
const obj = {key: 'value'};
obj.key = 'new value'; //This is allowed
const array = [1, 2, 3];
array.push(4); //This is allowed
const num = 3;
num = 4; //This will throw an error
const should be used for values that you know are not supposed to change throughout your script. This implies a level of security and also readability, since it is easier to understand the intention of your code if the variable's name and associated value don't change.
function calculateCircumference(radius) {
const PI = 3.14159;
const circumference = 2 * PI * radius;
return circumference;
}
By reading this article, you've invested 1.14 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
In JavaScript, 'const' is a special word that we use when we want to tell the computer to remember something and never change it. Think of it like naming your teddy bear. Once you've named your teddy bear, the name stays the same forever. It's the same in JavaScript with 'const', once you give a value a name with 'const', you can't change the value later.
Let's say you have a favorite number, and that number is 3. You can tell the computer to remember this by writing 'const favoriteNumber = 3;'. Now, wherever you write 'favoriteNumber' in your program, the computer will remember that it's 3.
const favoriteNumber = 3;
Remember how we said that once you've named your teddy bear, the name stays the same forever? It's the same with 'const'. If you try to change it, JavaScript will say 'No, you can't do that!' That's also a way for JavaScript to help us not accidentally change something we didn't mean to change.
const favoriteNumber = 3;
favoriteNumber = 4; // This will give an error!
By reading this article, you've invested 0.90 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
For more information about variable declaration and assignments in JavaScript, please check let in JavaScript
About author
Roman Y.
Senior Software Engineer at Nike
Certain articles only distributed to subscribers through email