Open In App

TypeScript Primitives: String, Number, and Boolean Type

In this article, we are going to learn about TypeScript primitives: string, number, and boolean Type in Typescript. These are the most used and basic data types. Below we will learn about string, number, and boolean in detail.

TypeScript Primitive DataTypes

Similar to JavaScript’s mostly used data types TypeScript has mainly 3 primitives



String Type

The string data type represents text or sequences of characters. Strings are enclosed in single or double quotes in JavaScript and TypeScript.

Syntax

let variableName: string = "value";

Where



Example: In this example, we will learn about string




let gfg: string = "GeeskforGeeks";
console.log(gfg)

Output:

Number Type

The number data type represents numeric values, both integers and floating-point numbers.

Syntax

let variableName: number = numericValue;

Where,

Example: In this example, we will learn about number




let num: number = 30;
let float: number = 19.99;
console.log(num)
console.log(float)

Output:

Boolean Type

The boolean data type represents a binary value, which can be either true or false. It is often used for conditions and logical operations.

Syntax

let variableName: boolean = true | false;

Where,

Example: In this example, we will learn about boolean




let isStudent: boolean = true;
let hasPermission: boolean = false;
console.log(isStudent)
console.log(hasPermission)

Output:

Reference: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#the-primitives-string-number-and-boolean


Article Tags :