JavaScript Code


JavaScript const – Uncaught TypeError: Assignment to constant variable


JavaScript const – Uncaught TypeError: Assignment to constant variable

This error occurs when we try assign a value the second time to a constant.

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

Solution

Constant variables cannot be reassigned. Second statement in the above code must not be written in the same block-scope.

const PI = 3.14159;