How to declare a constant in JavaScript

April 21, 2022

How to declare a constant in JavaScript

In this article we will learn how to declare a constant in JavaScript, in a native way of the language, which came with the ES6 version.

declare a constant in JavaScript thumb

Hey you programmer, ok? Let’s learn about variables and constants in JavaScript!

In previous versions of ES6 of JavaScript it was not possible to create constants, but there were ‘tricks’ that simulated it.

ES comes from ECMAScript, the source name of the JavaScript language

But with the coming of the ES6 version, the functionality arrived and the reserved word for this instruction is const

In this way, we can natively create a constant, that is, a variable that cannot have its value changed, only assigned when it is created.

See an example:

const nome = "Matheus";

In addition, the let statement, which creates a variable that can have its value changed, has also been added.

The two have one characteristic in common, their scope is created in any block

But that’s a topic for another post hehe

Beware of Compatibility

ES6 features are not yet fully standardized across all browsers

This can make your code non-functional in some of them, the most problematic is Internet Explorer

So if your application has to guarantee compatibility, you can look for some JS code transpiler

Like Babel, which will transform your code into standardized JS and then it can be used on all sites without any problems =)


In this article we learned  how to declare a constant in JavaScript

We use the const statement, which creates a variable whose values ​​cannot be changed (constant concept)

However, it is an ES6 feature, that is, it is not standardized in all browsers.

To solve this problem you can use a transipile, which will standardize your code to be used in any browser

Do you want to learn more about JavaScript and web development? Click here!

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x