Const keyword in Javascript
A const keyword used to declare an implicit type variable in JavaScript which is introduced in ES6 (2015). It is accessible in the scope in which it is define. A const variable must be initialize during the declaration which can not be changed until the scope in which it is define gets over.
Syntax
 1 // defining constant variable with initial value
 2 const <variable_name> = <value>
variable_name : A variable name is symbolic name given to the memory which will allows to access using that name
value : A value is initial value that can be assigned to the variable during the declaration which can not be changed
const keyword
 1 const num = 3.14; // declaring number constant
 2 const str = 'Hello world'; // define string constant
 3 	
 4 // constant array
 5 const array = [10, 20, 30];
In the above example, we have define array of integer which can not be changed once initialize but it is possible to add, remove or modify existing elements of an array. As we have define array as constant but not content of the array.

Operation on const Array and Object

Operation on const Array and Object
 1 // adding new element in array
 2 array.push(40);
 3 // updating existing element
 4 array[0] = 90;
 5 	
 6 // invalid operation, re-assignment is not allowed
 7 array = [60, 70, 80];
 8 	
 9 // constant object
 10 const object = {id = 1, name = 'Hello'}
In the above example, we have define java script object with properties which can not be changed once initialize but it is possible to add new property delete or modify existing property in the object.

Operation on const Javascript Object

Operation on const Javascript Object
 1 // changing existing
 2 object.name = 'world';
 3 	
 4 // adding new property
 5 object.salary = 20000;
 6 	
 7 // invalid operation, re-assignment is not allowed
 8 object = { id = 2, name = 'Test'}
 9 	
 10 const obj = {}; // defining javascript empty object
 11 	
 12 // const x value is 10
 13 const x = 10;
 14 {
 15    // const x value is String
 16    const x = 'String';
 17 }
If Javascript object define with const keyword, a object can not be changed once initialize with value but the property of the object can be add, remove or modify.
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us