Back to Course

0% Complete
0/0 Steps

Lesson 4 of 25
In Progress

# Understanding the Different Types in Typescript (e.g. Number, String, Boolean)

##### Yasin Cakal

Welcome to the “Understanding the Different Types in TypeScript” section of our course “Learn TypeScript”! In this article, we will cover the various types available in TypeScript, including numbers, strings, and booleans. By the end of this article, you should have a good understanding of these types and be able to use them effectively in your TypeScript programs.

## Numbers

TypeScript has two types for representing numbers: “number” and “bigint”. The “number” type represents a double-precision floating-point number, which is a number with a decimal point. The “bigint” type represents a large integer value. You can specify a number literal as a “number” by adding a decimal point or an exponent, or you can specify it as a “bigint” by adding the “n” suffix. For example:

let x: number = 10;
let y: bigint = 10n;

In this example, the variable “x” is of type “number” and the variable “y” is of type “bigint”.

TypeScript also supports the standard arithmetic operators for numbers, such as “+”, “-“, “*”, and “/”. For example:

let x: number = 10;
let y: number = 20;
let z: number = x + y; // 30

In this example, the variables “x” and “y” are both of type “number” and the result of their addition is also of type “number”.

## Strings

TypeScript has a single type for representing strings, called “string”. You can specify a string literal by enclosing it in single or double quotes. For example:

let x: string = 'hello';
let y: string = "world";

In this example, the variables “x” and “y” are both of type “string”.

TypeScript also supports the standard string operations, such as concatenation, interpolation, and slicing. For example:

let x: string = 'hello';
let y: string = 'world';
let z: string = x + ' ' + y; // 'hello world'

let a: number = 10;

#### How do you specify a boolean literal in TypeScript?

To specify a boolean literal in TypeScript, you can use the “true” or “false” keywords. For example:

let x: boolean = true;
let y: boolean = false;

#### What are the other types in TypeScript besides numbers, strings, and booleans?

The other types in TypeScript besides numbers, strings, and booleans include “null”, “undefined”, “void”, and “never”. The “null” and “undefined” types represent the absence of a value, with “null” representing the intentional absence of a value and “undefined” representing the default value for variables that have not been assigned a value. The “void” type represents the absence of a return value for a function. The “never” type represents a value that will never occur.