Constants in TypeScript
On this page
Constants
TypeScript supports constants using the const
keyword. Constants can hold primitive values such as strings, numbers, and booleans. Here’s an example:
A const
statement can appear anywhere a let
statement can.
Constant expressions can perform arithmetic with arbitrary precision.
A numeric constant doesn’t have a type until it’s given one, such as by an explicit conversion.
A number can be given a type by using it in a context that requires one, such as a variable assignment or function call. For example, here Math.sin
expects a number
.
To run the program, save the code in a .ts
file and use ts-node
to execute it, or compile it to JavaScript using tsc
and then execute it.
To run the code using ts-node
:
Or compile and run:
Now that we can run and build basic TypeScript programs, let’s learn more about the language.