Properties of the Number Type
The Number
object provides several useful properties.
Constant | Description |
---|---|
Number.MAX_VALUE | The largest number that can be represented. |
Number.MIN_VALUE | The smallest positive number that can be represented. |
Number.NEGATIVE_INFINITY | Represents the negative infinity value. |
Number.POSITIVE_INFINITY | Represents the positive infinity value. |
Number.NaN | Represents a value that is “Not-a-Number”. |
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // 5e-324
console.log(Number.NaN); // NaN
Methods of the Number Type
The Number
object provides a variety of methods for working with numbers. Let’s look at some of the most useful ones.
Conversion Methods
Method | Description |
---|---|
toString() | Converts the number to a string. |
toFixed() | Formats the number using fixed-point notation and returns a string |
toExponential() | Converts the number to a string in exponential notation |
toPrecision() | Formats the number to a string with the specified number of significant digits. |
toFixed()
The Number.toFixed()
method formats a number using fixed-point notation. It allows you to specify the number of decimals.
let num = 123.456789;
console.log(num.toFixed(2)); // '123.46'
console.log(num.toFixed(5)); // '123.45679'
toExponential()
The Number.toExponential()
method returns a string representing the number in scientific notation.
let num = 123456;
console.log(num.toExponential()); // '1.23456e+5'
console.log(num.toExponential(2)); // '1.23e+5'
toPrecision()
The Number.toPrecision()
method formats a number to a specific number of digits of precision.
let num = 123.456789;
console.log(num.toPrecision(4)); // '123.5'
console.log(num.toPrecision(7)); // '123.4568'
Method | Description |
---|---|
parseInt() | Converts a string to an integer |
parseFloat() | Converts a string to a floating-point number |
parseInt()
The Number.parseInt()
method converts a string to an integer. An optional numeric base can be specified (such as binary, octal, decimal, hexadecimal).
console.log(Number.parseInt('101')); // 101
console.log(Number.parseInt('101', 2)); // 5 (binary to decimal)
console.log(Number.parseInt('10.5')); // 10
parseFloat()
The Number.parseFloat()
method converts a string to a floating-point number. This is useful for interpreting and handling input data in text format.
console.log(Number.parseFloat('3.14')); // 3.14
console.log(Number.parseFloat('10.5abc')); // 10.5
console.log(Number.parseFloat('abc10.5')); // NaN
Sign and Comparison Methods
Method | Description |
---|---|
isInteger() | Determines whether the value is an integer |
isNaN() | Determines whether the value is NaN |
isFinite() | Determines whether the value is a finite number |
isInteger()
The isInteger()
method determines whether the passed value is an integer. This includes numbers without a decimal part.
console.log(Number.isInteger(42)); // true
console.log(Number.isInteger(42.5)); // false
console.log(Number.isInteger('42')); // false
isNaN()
Determines whether the value is NaN.
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN(5)); // false
isFinite()
The isFinite()
method determines whether the passed value is a finite number. This is useful for checking if a value is a number and not infinite.
console.log(Number.isFinite(42)); // true
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isFinite('42')); // false