const

The keyword 'const' was added in ECMAScript 2015 and can be used as an alternative to 'var'.
A variable declared using "const" has block scope.
This variable cannot be reassigned or redeclared.
The reason to use const is to indicate that the value of the variable does not change.

function myFunction() { 
   var x = 100;
   if (true) {
      const x = 200;
   }
   console.log(x); // x is 100
}

const variables are hoisted to the top of its scope.
const variables cannot be used before they have been declared.
const variables have the same value throughout the scope.


const myDate = '20.06.1978';  
const COLOR-RED = '#F00'

Warning - Objects, Arrays and other Collections

The value/contents can be changed.
Variables declares with 'const' are not completely immutable.

const myArray = [10,20,30]; 
myArray[1] = 200 // this is allowed


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext