JavaScript Code


JavaScript const


const keyword

In JavaScript, 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.