TypeScript Number
TypeScript, like JavaScript, supports numbers as a fundamental data type. These numbers can represent both integers and floating-point values. Understanding how to work with numbers efficiently is crucial for various tasks in TypeScript development. Let's look into a detailed explanation of TypeScript numbers with examples.
Number Representation
In TypeScript, all numbers are stored internally as 64-bit double-precision floating-point values according to the IEEE 754 standard. This means they can represent a vast range of values, from very small to very large, with some limitations in precision for certain values.
Number Literals
Numbers can be defined directly using literal values. TypeScript supports several formats for number literals:
- Decimal: 1, 10, 100
- Binary: 0b1010 (prefixed with "0b")
- Octal: 0o123 (prefixed with "0o")
- Hexadecimal: 0xAF (prefixed with "0x")
Number Declaration and Initialization
Numbers can be declared and initialized like this:
Running the TypeScript compiler (tsc) will generate the following JavaScript code:
TypeScript automatically infers the type based on the assigned value, but explicit type annotations, as shown, can be used for clarity.
Arithmetic Operations
TypeScript supports standard arithmetic operations like addition, subtraction, multiplication, and division.
Modulo Operator (%)
The modulo operator returns the remainder of a division.
Increment and Decrement
TypeScript supports increment (++) and decrement (--) operators.
NaN and Infinity
NaN represents "Not a Number," and Infinity represents positive infinity.
Number Type Annotations
TypeScript allows using type annotations to specify the expected type of a number variable or function parameter. This helps ensure type safety and avoid errors.
Typescript Number Properties
The Number object provides several properties related to numbers. These properties offer information about the range and precision of representable numeric values.
Number.MAX_VALUE and Number.MIN_VALUE
- Number.MAX_VALUE represents the maximum positive representable value in JavaScript.
- Number.MIN_VALUE is the minimum positive representable value greater than zero.
These values are useful for understanding the limits of numeric representation in JavaScript.
Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY
Number.POSITIVE_INFINITY represents positive infinity, while Number.NEGATIVE_INFINITY represents negative infinity.
These values are encountered in computations that result in overflow or underflow.
Number.NaN (Not a Number)
Number.NaN represents a value that is not a valid number. It is commonly used to indicate the result of an invalid or undefined mathematical operation.
Checking for NaN is often used to identify failed numeric operations.
Example Using Properties:Demonstrating the use of these properties in a practical example:
Running this code would display information about the maximum and minimum representable values, positive infinity, and "Not a Number."
Typescript Number Methods
The Number object provides various built-in methods for performing common operations on numeric values. These methods are accessible through the Math object. Let's explore some of the commonly used number methods with examples:
Math.abs(x: number)
Returns the absolute value of a number.
Math.round(x: number)
Rounds a number to the nearest integer.
Math.ceil(x: number): number and Math.floor(x: number)
- Math.ceil rounds a number up to the nearest integer.
- Math.floor rounds a number down to the nearest integer.
Math.random()
Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).
You can use this method to generate random numbers for various purposes.
Math.pow(x: number, y: number)
Returns the result of raising a number to a specified power.
Demonstrating the use of these methods in a practical example:
Conclusion
The number type is used to represent both integer and floating-point numeric values. It supports standard arithmetic operations, as well as additional functionalities provided by the Math object, offering methods for absolute values, rounding, random number generation, and more. The language also provides properties like Number.MAX_VALUE and Number.MIN_VALUE to understand the limits of numeric representation.