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 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


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