0% found this document useful (0 votes)
29K views3 pages

3 Primitive Types

The document discusses the primitive types in TypeScript including Number, Boolean, String, Void, Null, Undefined, and Enum. It provides examples of how to define and assign values to variables of each primitive type. Symbols are also introduced as a new primitive type in ES6 for representing unique identifiers.

Uploaded by

Abhishek Dabar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29K views3 pages

3 Primitive Types

The document discusses the primitive types in TypeScript including Number, Boolean, String, Void, Null, Undefined, and Enum. It provides examples of how to define and assign values to variables of each primitive type. Symbols are also introduced as a new primitive type in ES6 for representing unique identifiers.

Uploaded by

Abhishek Dabar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/4/2020 Playing | TypeScipt Fundamentals

Primitive Types

Primitive Types
The primitive types in TypeScript are the Number, Boolean, String, Void, Null, Undefined types
and Enum type.

Number

The Number primitive type is equivalent to JavaScript primitive type number, which represents
double-precision 64-bit floating-point values. The number keyword is used to define Number
type in TypeScript. TypeScript supports decimal, hexadecimal, binary and octal literals.

1. let nondecimal: number = 2;


2. let decimal: number = 2.4;
3. let hexadecimal: number = 0xf;
4. let binary: number = 0b100;
5. let octal: number = 0o7;

Boolean

The Boolean primitive type is equivalent to JavaScript primitive type boolean which accepts a
value that is either true or false. The boolean keyword is used to define a Boolean type in
TypeScript.

1. let yes: boolean = true;


2. let no: boolean = false;

String

The String primitive type is equivalent to JavaScript primitive type string and represents the
sequence of characters stored as Unicode UTF-16 code units. The string keyword is used to
define String type in TypeScript.

Just like JavaScript, TypeScript also uses double quotes (") or single quotes (') to surround string
data.

1. let name: string = "Kanishk Puri";


2. let site: string = 'www.dotnettricks.com';

Void

https://www.dotnettricks.com/player/typescript/typescript-fundamentals 1/3
4/4/2020 Playing | TypeScipt Fundamentals

The Void primitive


Primitive type represents the absence of a value. The void keyword is used to define a
Types
Void type in TypeScript but it is not useful because you can only assign null or undefined values
to it.

The void type is mostly used as a function return type that does not return a value or as a type
argument for a generic class or function.

1. function displayMeassge(): void {


2. console.log("Welcome to TypeScript!");
3. }
4. let unusable: void = undefined;

Null

The Null primitive type is equivalent to JavaScript primitive type null which accepts the one and
only value null. The null keyword is used to define Null type in TypeScript but it is not useful
because you can only assign a null value to it.

The Null type is a subtype of all types except the undefined type. Hence, you can assign nullas a
value to all primitive types, object types, union types, and type parameters.

1. let num: number = null;


2. let bool: boolean = null;
3. let str: string = null;
4. let n: null = null; //not useful, since you can assign only null value

Undefined

The Undefined primitive type is equivalent to JavaScript primitive type undefined and all
uninitialized variables in TypeScript and JavaScript have one and only value that is undefined. The
undefined keyword is used to define Undefined type in TypeScript but it is not useful because
you can only assign undefined value to it.

The Undefined type is also a subtype of all types. Hence, you can assign undefined as a value to
all primitive types, object types, union types, and type parameters.

1. le>let num: number = undefined;


2. let bool: boolean = undefined;
3. let str: string = undefined;
4. let un: undefined = undefined; //not useful, since you can assign only
undefined value

https://www.dotnettricks.com/player/typescript/typescript-fundamentals 2/3
4/4/2020 Playing | TypeScipt Fundamentals

Symbols
Primitive Types

A symbol is a new, primitive data type introduced in ES6 just like number and string. A symbol
value is created by calling the Symbol constructor.

1. let s1 = Symbol();
2. let s2 = Symbol("mySymbol");

Here is the list of some built-in symbols:

hasInstance- Used for determining whether an object is one of the constructor’s instance.
match- Used for matching the regular expression against a string.
iterator- Returns the default iterator for an object and called by the for-of loop.

search- Returns the index number within a string that matches the regular expression.
split- Splits a string at the indices that match the regular expression.

Enum

An enum is a way to give friendly names to a set of numeric values. The enum keyword is used to
define the Enum type in TypeScript. By default, the value of enum’s members starts from 0. But
you can change this by setting the value of one of its members.

1. enum Courses {TypeScript, Ionic, Angular2, NodeJS};


2. let tscourse: Courses = Courses.TypeScript;
3. console.log(tscourse); //0

In the previous example, we can start the enum’s members value from 2 instead of 0.

1. enum Courses {TypeScript=2, Ionic, Angular, Node};


2. let tscourse: Courses = Courses.TypeScript;
3. console.log(tscourse); //2

An enum type can be assigned to the Number primitive type, and vice versa.

1. enum Courses { TypeScript = 2, Ionic, Angular, Node };


2. let tscourse: Courses = Courses.Ionic;
3. let c: number = tscourse; //3

https://www.dotnettricks.com/player/typescript/typescript-fundamentals 3/3

You might also like