Introduction
A variable is like a container that stores data inside it. As we have a shoebox for storing shoes and a jewelry box for storing jewelry, similarly in programming, defining the type of data held in the container is essential. The exact type of container is used for a particular type of data.
Therefore, It is important to acquaint ourselves with the various data types used in Typescript.
Typescript consists of two major Data Types:
- Built-in Data Types
- User-defined Data Types
Built-in Data Types
As the name suggests, these are the primitive data types provided as default by Typescript. Currently, Typescript supports about 7 data types.
- Number
- String
- Boolean
- Void
- Null
- Unidentified
- Any
Number
This data type stores both integer and floating-point values (decimals). Furthermore, this data type can store hexadecimal, octal and binary values.
let int: number = 9; //Stores Integer Number
let dec_num: number = 3.414; //Stores Decimal Number
let hex: number = 0x37CF; //Stores Hexadecimal
let bin: number = 0b111001; //Stores Binary
let oct: number = 0o377; //Stores Octal Number
String
This data type stores a collection of characters, commonly known as a string of characters.
let deptName: string = “Coding Ninjas”; //Using Double Quotes
let empName: string = ‘Happy Singh’; //Using Single Quotes
let empID: string = `CNHS`; //Using Backtick
The use of Single, Double, and Backticks does not change the working of the code. However, backticks, also known as String Literals, can be used to produce multiple line spans and embed expressions by using ${variable_name }.
let literal_testing: string = `My name is ${empName}.
I work at ${deptName}.`;
Boolean
This data type is used to store True or False. It can be used as a flag during programming to realize the status of a component at a particular time interval.
let isAdult: boolean = false;
Void
This data type is used as a return type for a function that does not return any value. In other words, when no other data type is available, the void type is used.
function greet(): void {
console.log(“This is a greeting message”);
}
This function prints to the console and has nothing to return.
It is important to note that we cannot assign a void to a variable as it won't make sense to the compiler.
Null
This data type is used when a variable has no value assigned to it, or in other words, the value of that variable is undefined.
let int: number= null;
This way, we have assigned a variable of type number and name int but have not given it any values without introducing any errors in our code. This variable can later be given any value.
int=123;
Alternatively, we can use null to create a variable whose data type and value are undefined.
let int2: null= null;
Undefined
This data type is a primitive type that denotes all uninitialized variables.
let int3: number= undefined;
int3=456;
Any
This data type is a special type that can store all kinds of data.
let value: any= “Raman”; //String value stored
value= 100; //Value replaced by number data
value= true; //Value replaced by boolean
Any type is beneficial in cases where a function is expected to receive arguments that can be both number or string.
function test(a:any, b:any): {
console.log(a + b);
}
If the arguments passed are numbers, the output would be the addition of the two numbers; however, If the given arguments are strings, the output would be the concatenation of the two.