Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is const in JavaScript

Understanding const in JavaScript

The term const is short for "constant" in JavaScript, and it's used to declare a variable that cannot be re-assigned. As a beginner, you might be wondering, "Why would I ever want to use a variable I can't change?" Well, that's a great question, and by the end of this blog, you will not only understand the answer but also be able to use const confidently in your code.

The Basics: Declaring Variables with const

In JavaScript, you can create a variable using const in a similar way you would with var or let. Here's an example:

const myVariable = "Hello, world!";

In this code, we declare a variable named myVariable and assign it the string "Hello, world!". The key difference between const and the other two methods (var and let) is that once a const variable is assigned, you cannot change its value.

Trying to Reassign a const Variable

Here's an example of what happens when you try to reassign a const variable:

const myVariable = "Hello, world!";
myVariable = "Goodbye, world!";

If you try to run this code, JavaScript will throw an error. This is because myVariable is a const variable, and const variables cannot be reassigned.

const Doesn't Mean Immutable

One common misunderstanding is that const means the variable is immutable, or unchangeable. This is not entirely true. const only prevents reassignment of the variable, not modification of the variable's contents. For instance, if the const variable is an object or an array, you can still change its properties or elements.

const myArray = [1, 2, 3];
myArray.push(4); // This is okay

console.log(myArray); // Outputs: [1, 2, 3, 4]

In the above code, myArray is a const variable, but we were able to add a new element to it because we were modifying the array itself, not reassigning the variable.

Why Use const?

So, why would you want to use const? The primary reason is to make your code more predictable and easier to understand. When you declare a variable with const, you're making a guarantee that this variable will not be reassigned anywhere in the code. This can make it easier for you (and others) to reason about your code because you know certain variables will always have the same value.

Key Takeaways

To think of const in a non-technical way, imagine it as a locked box. Once you put something inside the box and lock it (const), you can't replace it with something else (reassign). However, if what you put inside the box is a bag with some items, you can still add or remove items from the bag (modify) without unlocking the box.

In conclusion, JavaScript's const keyword is a powerful tool in your coding arsenal that helps you write cleaner and more predictable code. It's not about restricting yourself, but about making explicit contracts in your code. So, the next time you're declaring a variable that doesn't need to be reassigned, consider using const. Happy coding!