Computer >> Computer tutorials >  >> Programming >> Javascript

Are there constants in JavaScript?


Yes, constant exist in JavaScript. The value of const remains the same; you cannot change and declare it again. Create a read-only reference with the const declaration.

const MY_CONSTANT = "some-value";

With const, you can declare local variables with block scope rather than function scope. Let’ say we have a constant variable a. After declaring a value like a constant variable, you can’t assign a new value to it.

// declared ‘a’ as a constant variable.
const a = 150;
// Assigning a new value to `a` will fail since ‘a’ is a constant variable
a= 0;
a++;
// The below declarations won’t work
var a = 0;