Monday, November 26, 2018

Understanding Const In Javascript With Examples

This tutorial explains how to use const keyword in javascript. const statement values can be assigned once and they cannot be reassigned. The scope of const statement works similar to let statements.

  • const :- It has block level scoping but cannot change the value reference. 
Point To Remember :
  • Variables defined with const behave like let variables, except they cannot be reassigned.
  • we cannot change constant primitive values, but we can change the properties of constant objects. 
Understanding Const In Javascript With Examples

Example - 1 :
const PI = 3.141592653589793;
PI = 3.14;      // This will give an error
PI = PI + 10;   // This will also give an error

Output:-
-------------------------
Error: Assignment to constant variable.

Best Practice to remember while working with const keyword in javacript :

  •  JavaScript const variables must be assigned a value when they are declared:

Lets see the below examples for const keyword :

Incorrect Approach :
//Incorrect

const PI;
PI = 3.14159265359;

Correct Approach :
//Correct

const PI = 3.14159265359;

Block Scope :

Lets see the example for block scope using const keyword. Declaring a variable with const is similar to let when it comes to Block Scope.

var x = "skptricks";
{ 
    const x = 34;
    console.log(x)
}
console.log(x)

Output:-
-------------------------
> 34
> "skptricks"

Working With Primitive Values :

If we assign a primitive value to a constant, we cannot change the primitive value:

const PI = 3.141592653589793;
PI = 3.14;      // This will give an error
PI = PI + 10;   // This will also give an error

Output:-
-------------------------
Error: Assignment to constant variable.

Working With Constant Objects :

You can change the properties of a constant object:

Example -1 : 
Here we are changing object values :
// You can create a const object:
const employee = {name :"Sumit Kumar", salary:60500, employeeno: 23, location : "india"};

// You can change a property:
employee.name = "Amit Kumar";
console.log(employee.name)

// You can add a property:
employee.location = "USA";
console.log(employee.location )

Output:-
-------------------------
> "Amit Kumar"
> "USA"

Example - 2 :
Just remember we can't not re-assign/change the const object.
// You can create a const object:
const employee = {name :"Sumit Kumar", salary:60500, employeeno: 23, location : "india"};

// You can change a property:
employee = {};

console.log(employee)

Output:-
-------------------------
Error: Assignment to constant variable.

Working With Constant Arrays :

You can change the elements of a constant array:

Example -1 : 
Here we are changing array values :
// You can create a const object:
const employee = [ "A", "B", "C", "D", "E" ]

// You can change a property:
employee[0] = "Z"; //changing array value

employee.push("F" ) ; // adding new entery

console.log(employee)

Output:-
-------------------------
> Array ["Z", "B", "C", "D", "E", "F"]

Example - 2 :
Just remember we can't not re-assign/change the const array .
const employee = [ "A", "B", "C", "D", "E" ]

employee = ["AA", "BB"] ;

console.log(employee)

Output:-
-------------------------
Error: Assignment to constant variable.

This is all about const In Javascript. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.


No comments:

Post a Comment