What is const in TypeScript?

What is const in TypeScript?

In this article, we are going to see what is const in TypeScript. So be ready to learn something new, before we have the var keyword to declare variables in TypeScript. But after the ES6 update we get two more new keywords to declare variables: let and const.

What is const in TypeScript?

So const is a keyword which is come from the word constant. Which we use to declare variables in TypeScript. There are two other keywords that we use to use to create variables and they are var and let. Variables that are created using const are constant which means we cannot re-assign or re-declare them after the declaration.

And even if we tried to re-assign or re-declare a value of a variable created using the const keyword we will get an error. like below shown in the code. You can learn more const here

const birthYear:number = 2002;
console.log(birthYear); // it will log 2002 on the console.
birthYear = 2003; // it will show an error Cannot assign to 'birthYear' because it is a constant.
Code language: TypeScript (typescript)

In short, when we declare const variable it becomes immutable. Or it becomes a read-only value we can read the value but cannot update or change it. So keep in mind that use const when you know that the value of a certain variable will not change in your program. and use let when you need to update or re-assign the value of your variable throughout your program.

Declare const in a single statement

Like the variables which are declared with var and let keywords. We can create the variable without giving them a value first and then when we need to add a value to them we can initialize them where we need the value. But that can be only possible when we are using the let, var keywords.

consider the following code example.

var dayName:string; // Declared a variable without assigning a value to it.
dayName = 'sunday'; // assigned a value to dayName after initializing it..
console.log(dayName); // here we will get sunday log on the console.
Code language: TypeScript (typescript)

But with const we can not do that. we have to initialize and assign a value to a declared variable right after we initialize them. Otherwise, we will get an error when we try to do that. Consider the following code example.

var monthName:string; // Missing initializer in const declaration.
Code language: TypeScript (typescript)

As I said when creating a variable with the const keyword you need to declare and initialize the variable right after you create it. So that’s what you have to consider when working with const.

Const is block-scoped.

The const declaration follows the same syntax as the var declaration. Unlike variables declared with var, variables declared with const have block scope. This means the scope const declared variables are limited to the block where they are declared for example function, if-else block, and for-loop block. And variables declared with var is a functional scope which means we can access them inside the body of our function.

Consider the following code example.

function demo() {
 if(true) {
   const num1:number = 22;
   console.log(num1); // It will log 22 on the console.
 }
  console.log(num1); // It wll throw an error num1 is not defined.
}
demo();
Code language: TypeScript (typescript)

In the code example, we have a demo function which has a if statement inside the if block we created a num1 variable and assign a value of 22 to it and we are logging the num1 variable to the console right in the if block. That is fine because of the block scope we can do that.

But then we are trying to log the num1 variable outside of the if block, we can’t do that because we are using const and const is block scoped which means we cannot be able to use the num1 variable outside of the if block. That’s why we get the error on line 6.

Conclusion

In this article, we learned about what is const in TypeScript. And some basics of, block-scope and what you need to consider when creating variables with const. If you don’t understand any topics or you want to learn something new then check out some pretty good courses on Codedamn. Also, check out the playground for coding practices.

Sharing is caring

Did you like what Amol Shelke wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far