Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptExplain Constants in ES6

Explain Constants in ES6

JavaScript is the world’s most popular lightweight, interpreted compiled programming language. It is also known as a scripting language for web pages. It is well-known for the development of web pages, many non-browser environments also use it. JavaScript can be used for Client-side developments as well as Server-side developments.

The Const Keyword: While programming, we come across some situations in which we want to declare a variable whose value remains constant throughout the program. One such example would be the value of pi remains constant always. ES6 supports one such declaration which can be declared using the word const.

The syntax of declaring a const variable is:

const CONST_NAME = value;

 

Properties of Const:

  • The variable declared using the word const is immutable.
  • Variables declared with const are block-scoped.
  • Variables declared with const cannot be reassigned.
  • Variables declared with const must be assigned a value as soon as they are declared.
  • Variables declared with const cannot be used before they are declared.

The below examples will show the various properties of using the const keyword.

Example 1: In this example, an error will be thrown as “Attempting to override ‘i’ which is a constant”. Therefore, this shows that the variable declared with const cannot be overridden.

Javascript




<script>
    const myFunc = function () {
        const i = 5;
  
        try {
  
            // The below line will throw an error
            i = i + 2;
        }
        catch (error) {
            console.log(error)
        }
    }
</script>


Output:

TypeError: Assignment to constant variable.

Example 2: The below statement will also show a “syntax error” as the value of the const variable must be defined only at the time of declaration.

Javascript




// Will throw syntax error as
// no value has been assigned        
const MY_VAR;


 

Output:

SyntaxError: Missing initializer in const declaration

Example 3: The below statements also throw an error as ” Uncaught syntax error” as we are trying to declare a use the variable name which is already declared as a const.

Javascript




// Declaring multiple const values
// with the same variable
const pi = 3.15;
const pi = 2.6;


Output:

SyntaxError: Identifier 'pi' has already been declared

Declaring const with objects: By declaring an object as a constant, we can manipulate the internal properties of the object.

Example 4:

Javascript




const pair = {x:30, y:60};
  
// Updating the x and y values
// of the object
pair.x = 10;
pair.y = 90;
  
console.log(pair);


Output:

{
  x: 10,
  y: 90
}

The values of the properties of a const object can be manipulated, however, you cannot reassign the const object as shown in the below example.

 

Example 5:

Javascript




const pair = { x: 30, y: 20 }
  
// Will throw an error that
// pair has already been declared
const pair = { a: 35 }


Output:

SyntaxError: Identifier 'pair' has already been declared

Example 6: One can also add a new property to a const object as below.

Javascript




const pair = { x: 30, y: 20 }
  
// Adding a new property
// to the pair
pair.z = 67;
  
console.log(pair);


The output would be something like this :

{
  x: 30,
  y: 20,
  z: 67
}

Const with Loops: ES6 supports the usage of const in for-of loops where new binding is created for each iteration.

Example 7:

Javascript




const arr = ['hi', 'how', 'are', 'you'];
  
// Looping through all the elements
// of the array
for (const ele of arr) {
    console.log(ele);
}


Output:

hi
how
are
you

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments