In a programming language, we deal with various small units of data, such as numbers
, sting
, and Boolean
values. TypeScript supports these types of data, as in JavaScript, with enumeration and structure types.
The boolean
data type can hold either true
or false
. Declaring and initializing this data type is quite easy and is illustrated as follows:
let isSaved: boolean = false;
Here, the isSaved
variable is declared as boolean
and is assigned with the value false
. If the developer assigns a string value to the isSaved
variable by mistake, the TypeScript will show an error and highlight the statement.
The number data type holds floating-point values. Similar to JavaScript, TypeScript considers all numbers as floating-point values. Declaring and initializing the number data type variable can be done as follows:
let price: number = 101;
Here, the price
variable is declared as number
and is assigned the value 101. Number type can hold values such as decimal, binary, hexadecimal...