×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

Constants in JavaScript

JavaScript constants: Here, we are going to learn about the constant in JavaScript, how to declare the constants in JavaScript?
Submitted by Siddhant Verma, on October 09, 2019

JavaScript Constants

Before ES15, the only way to declare variables using the var keyword. JavaScript's inbuilt feature of hoisting variables could be carried out using the var keyword. If you're unfamiliar with variables in JavaScript, have a glance at Variables in JavaScript article on the website. If you wish to know what hoisting is, read through Hoisting in JavaScript.

Today we'll look at constants. Variables after ES15 could be declared in two ways - let and const. Before we dive deeper into const, let's understand what a constant is?

Constants in most languages are something that retains their value during their block or wherever their life persists. In JavaScript, constants are values that we cannot modify later directly. This means if we declare a constant with a certain value, assigning some other value later in the program would result in an error.

    const a=10;
    a=25;

Output

Uncaught TypeError: Assignment to constant variable. at <anonymous>:1:2

We get a TypeError.

Declare a constant

We declare a constant using the keyword const.

const follows block scope. Consider the following,

var t=10;	//Value of t here is 10
{
	const t=12;	//Value of t here is 12
}
//Value of t here is 10

Also, constants declared using the const keyword must be initialized and declared at the same time.

    const g=9.8;
    //is correct, but

    const g;
    g=9.8i
    //is incorrect.

However, an important catch to note that the const keyword is not what it looks like. The more accurate statement regarding const would be that it defines a constant reference to a value. This means that primitive values assigned to a variable using the const keyword cannot be altered but with objects, we are bound to no such restriction.

If we declare an object with the const keyword, we can later change it by giving it new properties.

    const person={
        Name: 'Fuzzy Sid',
        Age: 20
    }
    person.gender='Male';

Adding a new property to a constant object does not yield us an error.

Similarly, we can change the elements of an array defined using the const keyword.

    const arr=[1,2,3,4];
    console.log(arr);       //[1,2,3,4]
    arr[0]=0;
    console.log(arr);       //[0,2,3,4]
    arr.push_back(5);
    console.log(arr);       //[0,2,3,4,5]

However, we cannot reassign new values to the array.

    arr = [9,8,7,6]

Output

Uncaught TypeError: Assignment to constant variable.at <anonymous>:1:4

Would give an error. But the trick around this would be to individually change the values of the array or maybe run a loop for the same.

    for(let i=0,cnt=9; i<arr.length; i++,cnt--){
        arr[i]=cnt;
    }

Remember that const variables are not supported in versions of Internet Explorer before 10 and they are not hoisted. So you cannot use them before the declaration.

JavaScript Tutorial »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.