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.
1
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
1 const num = 3.14;
2 const str = 'Hello world';
3
4
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
2 array.push(40);
3
4 array[0] = 90;
5
6
7 array = [60, 70, 80];
8
9
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
2 object.name = 'world';
3
4
5 object.salary = 20000;
6
7
8 object = { id = 2, name = 'Test'}
9
10 const obj = {};
11
12
13 const x = 10;
14 {
15
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.
Related options for your search