Typescript

How to Declare a Constant in TypeScript?

TypeScript provides a “const” keyword that helps us declare the unchangeable variables. It makes the specified variable, object, array, and other data structures constant as their values cannot be modified or changed once they have been initialized. It is useful for the declaration of unique data to ensure that it will not change throughout the source code once it has been set.

This post explains how to declare a constant in TypeScript.

How to Declare a Constant in TypeScript?

To declare a variable, object, or array as constant utilize the “const” keyword. This keyword specifies that the given variable is constant which means that its value cannot be changed once it has been assigned. It treats the specified variable/object as a constant at compile time.

Let’s use the “const” keyword practically to declare a variable, object, and array constant. Before heading into the practical implementation, note that the following commands will be used to transpile the “.ts” file containing the code and to execute the automatically generated “.js” file:

tsc main.ts //Compile .ts File
node main.js //Run .js File

The file name mentioned in the above-stated commands can be modified according to your file name.

Example 1: Declare a Constant Variable
This example uses the “const” keyword to declare a variable constant by following this code snippet:

const a = 100;
a = 50;
console.log(a);

In the above code snippet:

  • The “const” keyword initializes “a” as a constant variable.
  • Next, the constant “a” variable value is modified.
  • Lastly, the “console.log()” method displays the “a” variable value.

Output

It is observed that during compilation an error is generated that specifies the “a” variable value cannot be changed because it is a constant.

Example 2: Declare a Constant Object
This example declares an object constant with the help of the “const” keyword:

const user = {
 name: 'Herry'
};
 user={
 name: 'Johnson'
}

In the above code lines:

  • The “const” keyword declares the “user” object constant.
  • In its body, one field name is specified that contains a string value.
  • Next, make another object with the same name as the constant “user” object having one field “name”.

Output

The terminal generates the same error as produced for a constant “variable” because the declared “user” object is constant which does not allow to make another object like this.

Example 3: Declare a Constant Array
This example declares an array constant also utilizing the “const” keyword. It works the same as the constant object:

const languages = [ 'HTML', 'JavaScript', 'NodeJS' ];
languages = [];

In these lines of code:

  • The “const” keyword initializes a constant array named “languages”.
  • Next, another empty array is also initialized with the same name “language”.

Output

The compiler generates the same error as for constant variables and objects which confirms that a constant array cannot be modified.

Note: The const keyword only declares the structure of the “object” and “array” as constant, not their content. Therefore, their content can be changed if they are declared as constant. If the user needs to make their data i.e. properties/fields constant then use the Typescript read-only Utility Type.

Conclusion

In TypeScript, to declare a variable, object, and array constant use the “const” keyword. This keyword restricts the users to change the constant variable content when it has been set. On the other hand, it does not generate any error in changing the constant object and array content. This is because it only declares the object structure constant, not its content. This post deeply explained how to declare a constant in TypeScript.

About the author

Areej Nadeem

I am a technical author holding a Bachelor’s degree in Computer Science. I am passionate about writing and learning new technologies and sharing my knowledge with the rest of the world.