Can you actually change the value of a constant variable in TypeScript? The answer is yes you can and I am going to show you how to do it!
Constant value is not supposed to change because if you try to change the constant value in TypeScript you will get an error.For example:-
const item1 = 1 item1 = 2 /* not allowed *.
But actually, if you declared that variable with the same name in different scope with the ‘let’ keyword then you can actually change the value of the previously declared variable to another value as well as another type.
const item1 = 1 if (item1 == 1) { let item1 = "Hello World!" as string console.log(`${item1}`) /* Hello World! */ } console.log(`${item1}`) /* 1 */
You used ‘constant’ variable in your program because you only want to make sure that variable cannot get changed throughout the entire program thus there is no reason for you to later change it to another value or type! Therefore this article will only serve as a guild for those that wants to know whether a constant value can be changed or not in TypeScript.