JavaScript: Using const

In a previous post, I described the difference between var and let in JavaScript. In this post, I’ll be explaining what const means in JavaScript and why we would use it and how.

The const keyword in JavaScript means a constant, i.e. it’s meant to stay constant and not change in the program. As such, it’s a read-only variable. This is what makes it different from let. You won’t be able to change its value if you try to assign it again and you also won’t be able to declare it again. If you try to you’d just get an error.

Like let, it’s also block scoped. If there’s a variable you don’t want to assign again then you should use const. This is because it prevents you from accidentally assigning a variable that’s not meant to change or that’s supposed to stay constant.

If you have further questions feel free to post them below.

Leave a comment