JavaScript Code

JavaScript const


const keyword

const keyword is used to declared constants that cannot be changed.

const PI = 3.14159;

constant variables cannot be redeclared.

const PI = 3.14159;
const PI = 3.1415926; //Uncaught SyntaxError: Identifier 'PI' has already been declared

constant variables cannot be reassigned.

const PI = 3.14159;
PI = 3.1415926; //Uncaught TypeError: Assignment to constant variable

constant variables have block space.

{
  const PI = 3.14159;
}
{
  const PI = 3.1415926;
}

this code is valid. PI declared in the first block can be used and visible only to the first block. PI declared in the second block can be used and visible only to the second block.