Constant in JavaScript Tutorial

In this section, we will learn what JavaScript const keyword is and how to use it.

Const in JavaScript

In JavaScript, sometimes we have a variable (AKA identifier) that is assigned once and its value won’t change throughout the life of the program.

Basically, the identifier is constant. Meaning its value stays the same no-matter what state the program is in.

Now, in order to make sure the value of that identifier does not change at all (intentionally or un-intentionally), we can create that identifier by the JavaScript `const` keyword.

The JavaScript const keyword stands for constant and is used when we have an identifier that takes one value and that value won’t change throughout the lift of a program.

That means if we attempt to change the current value of such identifier, we will get a runtime error which might lead to crashing the program.

How to Declare Constant in JavaScript: Const Syntax

In order to create a constant identifier in JavaScript, we first start with the `const` keyword and then, on the right side of this keyword, we set the name of the identifier.

const constant_identifier = “value”;

Note: you need to assign a value right when the constant identifier is created. Otherwise you’ll get an error. Basically, the creation and value assignment of a constant identifier happen in the same statement.

Example: creating constant variable in JavaScript

const constant_id = 1000; 
alert(constant_id);
constant_id = 200;//Error

Here we have one constant identifier named `constant_id` and as you can see, the value is assigned right where the declaration is happening.

Also note that in the third statement, we’ve tried to change the value of this identifier! But the moment a computer reaches to this statement, an error will arise because the identifier is a constant and no change of value is allowed here!

Difference between Constant and Variable: const vs var

Well, those identifiers that are created from the `var` keyword are free to change their values at any time while the program is running.

But on the other hand, those identifiers created from the `const` keyword are locked with only one value throughout their lifetime in a program.

JavaScript const and Objects

If you’re not familiar with JavaScript Objects, please check the related section first.

First of all, you should know that objects are stored in a location called heap and only their memory address (the location in the heap where the object is stored) is assigned to a variable.

So if you have a constant identifier in your program that has an object assigned to it, in reality though, that identifier only has the memory address of the target object!

So what does this mean?

It means, if there are properties in that object, you can still invoke them and change their values if needed!

Now to get into more details:

When calling a property of an object, we’re basically calling another memory location and saying in that memory location we want to store something different from what is already stored there!

Now, if you think about it, we didn’t change the memory address that is stored in the constant identifier at all! We just use this address to reach to another memory address so to change something in there!

That’s why a constant identifier can have an object assigned to it and still be able to change the values of its properties without any sort of error.

But if you try to change the value of a constant identifier to anything else, that will cause you an error. Because this time, we’re attempting to change the value of the identifier itself!

Example: creating const object in JavaScript

const obj = {
   name : "Jack",
   lastName: "Doe"
}
obj.name = "John";
alert(obj.name);

As you can see, we’ve changed the value of the `name` property to “John” and this change occurred without any error.

JavaScript const and Arrays

In this section, we’re expecting you’re familiar with JavaScript Arrays.

Arrays are object as well, so if we create an array in a program, again that array is stored somewhere in the heap memory and just its address will be stored in an identifier.

This means even if the identifier is a constant one, we’re still able to change the elements in an array without any error.

The error only happens if we try to change the value of a constant identifier to something else. But if we use the value of such identifier to access another memory address, then it is totally possible to change the value of that second memory location. This is because we’re not affecting the value of the constant identifier itself.

Example: creating const arrays in JavaScript

const arr = [0,0,2,3,4];
arr[1] = 1; 
alert(arr[1]);

In this example, the identifier `arr` has the memory address of an array. Note that this array is stored somewhere in the heap memory and the `arr` has only its memory address.

Now we used the address stored in the `arr` to reach to the address of the second element in the array via `arr[1]` and hence we’re able to change the value of this second memory location to anything we want (in this case the value 1).

At the end, we didn’t change the value of the `arr` identifier but only used its address to reach to a second memory location.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies