TypeScript Constant with Program Example

What are Typescript Constants (Const)

Typescript Constants are like regular variables, but once you set their value, you can’t change it.

We create them using the word ‘const’. Just like “let”, they only exist within the block where you define them.

Once you’ve set a constant, you can’t change its value or create a new one with the same name. This ‘const’ feature comes from the

How to write a constant in the TypeScript?

Declaring a constant in TypeScript is straightforward. Here are the steps:

  1. Keyword: Use the const keyword to declare a constant.
  2. Name: Choose a name for your constant. This should be in camelCase or UPPER_CASE.
  3. Value: Assign an initial value to the constant. This is mandatory because once a value is assigned to a constant, it cannot be changed.
  4. Scope: Remember that constants are block-scoped, meaning they only exist within the block where they are declared.

Here’s an example:

{
    // Declare and initialize a constant
    const Grades = 99;

    console.log(Grades); 
}
// Grades is not accessible here, outside of the block

In our given example above, Grades is a constant that is initialized with a value of 99.

It can only be accessed inside the block where it’s declared. You will get an error if you try to access it outside of this block.

This demonstrates the block scope of constants in TypeScript. Remember, the initial value is a must when declaring a constant.

Let’s check an example code of const in a block-scoped in TypeScript:

const Grade: number = 90;       // This is a global constant

if (true) {
  const Grade: number = 99;     // This is a new constant that only exists within this block
  console.log(Grade);  // This will output 99, because it refers to the constant within this block
}
 
console.log(Grade);     // This will output 90, because it refers to the global constant

In the if block, a new constant Grade is declared and initialized to 99. This constant is only accessible within the if block.

When console.log(Grade) is called inside the if block, it refers to this block-scoped constant, so 99 is printed.

Outside the if block, when console.log(Grade) is called, it refers to the global constant Grade that was initialized to 90, so 90 is printed. The block-scoped constant Grade from the if block is not accessible here.

This demonstrates that const in TypeScript (and JavaScript) is block-scoped: they are only accessible within the block in which they are declared.

If a constant with the same name is declared in a nested block, it does not affect the value of the constant in the outer block. Each block gets its own unique Grade constant.

This is a key feature of TypeScript’s block scoping. It helps prevent bugs that can occur due to variable hoisting and accidental reassignment.

This is especially important for const because it’s read-only, meaning you can’t change its value after it’s been set.

If you try to do so, TypeScript will throw an error. This makes const a good choice when you have a value that you know won’t need to change.

You can’t change the value of a constant

This simply means once you’ve set a value to a constant, you can’t assign it a new value later.

It stays the same. This is why you can’t put a constant on the left side of an assignment because that’s where you’d put something you want to change.

Let’s take a look at this example:

const Grades = 90;
Grades = 90; // This will cause an error

As you can see, Grades is declared as a constant and assigned the value 90.

When you try to reassign Grades to 99, it will cause an error because Grades is a constant and its value can’t be changed once it’s been set.

You can’t use const before it’s declared

If you try to use a constant before it’s declared, you’ll get an error. This is because the constant is in a “temporal dead zone” from the start of its block until it’s declared.

For example:

console.log(Grades); // This will throw an error

const Grades = 90 ; // Declaration of the constant

In the above code, we’re trying to log the value of Gades before it’s declared.

This will result in a ReferenceError because Grades is in a “temporal dead zone” from the start of the block until it’s declared.

This means that Grades isn’t accessible before its declaration.

After the constant Grades is declared, it can be accessed without any issues.

Redeclaring const results in an error

If you try to declare a constant that’s already been declared, you’ll get an error.

For example:

const Grades = 90;
console.log(Grades);
 
const Grades = 99;  //This part will cause the error because Grades was already declared with const
                     
console.log(Grades);

Conclusion

Constants in TypeScript are incredibly useful. They enhance the stability of your code, making it easier to read and maintain.

By understanding how they function and utilizing them effectively, you can significantly improve your TypeScript code.

Use const to declare variables. If you need to modify the variable’s value, use let. However, remember that if your const is an object, its properties can be altered.

I hope this article has helped you understand TypeScript Constant.

If you have any questions or inquiries, please don’t hesitate to leave a comment below.

Leave a Comment