header ad

Data Types in TypeScript

Data Types in TypeScript

Data Types in TypeScript – We’ll learn about the various forms of data that can be stored in data containers in this TypeScript course. In TypeScript, we learn about basic types including numbers, booleans, strings, void, null, undefined, and any.

What are data types

Data Type is a data attribute that informs us about the data’s value. It doesn’t matter if it’s a number, string, boolean, or something else. The operations we can do on data are determined by its kind.

header ad

Take a look at the sample below. The code defines two numbers and uses the + operator to add them. The runtime recognizes the data type as a number and performs the arithmetic addition on it correctly.

let num1=10
let num2=10
console.log(num1+num2)   //20  Numbers are added

The code below is fairly similar to the code previously mentioned. We use the + operator to join two strings instead of two numbers. The interpreter correctly recognizes the data type as a string in this case, and hence connects the two.

let str1="Hello"
let str2="world"
console.log(str1 + str2)  //HelloWorld       Strings are joined

How does the interpreter know when to add and when to join the numbers? Examining the data type is a good place to start. When both variables are numbers, the interpreter adds them together. It joins them if one of them is a string.

The following data types are available in TypeScript.

  • Generics
  • Decorators
  • Static types
  • Build-in-types or Primitive Types
    • Number
    • String
    • Boolean
    • Void
    • Null
    • Undefined
    • Never
    • Any

The number data type

Both integers and fractions are represented by the number type. In TypeScript, the number datatype is 64-bit floating-point values. The hexadecimal and decimal literals are also supported by Typescript. It also supports ECMAScript 2015’s binary and octal literals.

header ad
// integer number
let num1 : number = 83;

// floating point number
let num2 : number = 3.14;

// hexadecimal number
let num3: number = 0x37CF; 

console.log("int:", num1);
console.log("float:", num2);
console.log("hexadecimal:", num3);

The String data type

A string is a collection of Unicode characters that can be used to create words or phrases. In TypeScript, we’ll represent the text with the string data type. We use single or double quotation marks to enclose string literals in our scripts.

let message = "Hello World"
let name = 'Jone Doe'

console.log("message:", message);
console.log("name:", name);

The boolean data type

The string and numeric data types can have an infinite number of values, however, the Boolean data type can only have two. They are "true" and "false," respectively. A Boolean value is a true value that indicates whether or not a condition is true.

let isChecked: boolean = false;  

The Void Return Type

The void type is used to represent functions that do not return a value and is used as a function return type. Our function returns nothing and simply prints a message on the console. To explicitly specify that the function does not return a value, we give it the void type in the function header.

function name():void {

  console.log("Hello, World!");

The Null Data Type

Null denotes a variable with no defined value. The only value that the Null accepts is null. The Null keyword in TypeScript is used to specify the Null type, but it is useless because we can only assign a null value to it.

let num: number = null;  
let bool: boolean = null;   
let str: string = null;  


  • The different forms of data that can be stored are represented by data types.
  • Integers, hexadecimal, octal, binary, and floating point numbers can all be stored in the number type.
  • Words and sentences are formed by joining together several unicode characters.
  • The boolean type is a named constant with two possible values: true or false.
  • The return type of a function is set to void when it returns no values.

Similar Posts:

Leave a Comment

Your email address will not be published.