TechMentorAI

const

in JavaScript

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.

Imagine const in JavaScript

Syntax of const in JavaScript

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;

Behavior of const in JavaScript

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

Using const in JavaScript

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;
}

No Time to Read? Learn on the Go!

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.

Imagine const in JavaScript

How to use const?

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;

What happens if we try to change a const?

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!

No Time to Read? Learn on the Go!

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

Why did I decide to launch this website? Drawing from my rich background in interviewing candidates for a variety of developer roles, I've observed a common pattern: many applicants share similar deficiencies in their knowledge during technical interviews. Motivated by this insight, I established this website with the aim of assisting developers in securing their ideal job. Through straightforward and concise articles, my goal is to not only deepen your understanding of programming language nuances but also to equip you with the insights needed to deliver the precise answers interviewers expect. Essentially, you'll be providing the correct response. I encourage you to spread the word about this site on social media platforms. Echoing the wisdom of an Armenian saying: "Do good and cast it into the water."

EXCLUSIVELY

Certain articles only distributed to subscribers through email