EN
Nopass
Nopass

Data Types in TypeScript

Data types in TypeScript are a fundamental part of the language, allowing you to define what kinds of data can be used in your program. TypeScript provides various built-in data types and the ability to create custom data types. In this post, we will cover the basic data types in TypeScript, provide examples, and explore union types.
1. Numbers
Numeric data types in TypeScript include integers and floating-point numbers.
To declare a variable with the number data type, you can use the number keyword. Here's how you declare and initialize a number variable:
2. Common Operations
Arithmetic Operations
You can perform standard arithmetic operations with number variables, such as addition, subtraction, multiplication, and division:
Increment and Decrement
You can use the increment (++) and decrement (--) operators to modify number variables:
3. Number Methods
toFixed(digits: number)
The toFixed method allows you to format a number with a fixed number of decimal places and returns it as a string:
toExponential(fractionDigits?: number): string
The toExponential method returns a string representation of a number in exponential notation. The fractionDigits parameter, which is optional, specifies the number of digits to include after the decimal point.
toPrecision(precision: number): string
The toPrecision method returns a string representation of a number using the specified precision. The precision parameter determines the total number of significant digits in the result.
toString(radix?: number): string
The toString method converts a number to its string representation. The radix parameter, which is optional, specifies the base for representing the number. By default, it's base 10.
isNaN(): boolean
The isNaN method checks if a number is "Not-a-Number" (NaN). It returns true if the value is NaN, and false otherwise.
isFinite(): boolean
The isFinite method checks if a number is finite (i.e., not NaN, Infinity, or -Infinity). It returns true for finite numbers and false for NaN or infinity values.
parseFloat(string: string): number
The parseFloat function parses a string and returns a floating-point number. It stops parsing as soon as it encounters an invalid character.
parseInt(string: string, radix?: number): number
The parseInt function parses a string and returns an integer. The optional radix parameter specifies the base for parsing. If omitted, it assumes base 10.
These methods and properties associated with number values in TypeScript allow you to perform various operations and format numbers as needed in your applications. Understanding these functions is crucial for working with numeric data effectively.

Subscription levels

No subscription levels
Go up