JavaScript Number Methods

JavaScript provides a variety of built-in methods for working with numbers. These methods allow you to perform mathematical operations, convert between different numeric formats, and get information about number values.

1. `toFixed()` Method

The `toFixed()` method formats a number using fixed-point notation and returns it as a string. You can specify the number of digits after the decimal point.


// Example of toFixed()
const num = 123.456789;
console.log(num.toFixed());    // "123"
console.log(num.toFixed(2));  // "123.46"
console.log(num.toFixed(5));  // "123.45679"

    

2. `toPrecision()` Method

The `toPrecision()` method formats a number to a specified length and returns it as a string. This method can be useful for controlling the total number of significant digits.


// Example of toPrecision()
const num = 123.456789;
console.log(num.toPrecision());    // "123.457"
console.log(num.toPrecision(5));   // "123.46"
console.log(num.toPrecision(8));   // "123.45679"

    

3. `toExponential()` Method

The `toExponential()` method returns a string representing the number in exponential notation. You can specify the number of digits after the decimal point.


// Example of toExponential()
const num = 123456;
console.log(num.toExponential());    // "1.23456e+5"
console.log(num.toExponential(2));   // "1.23e+5"
console.log(num.toExponential(4));   // "1.2346e+5"

    

4. `Number.isInteger()` Method

The `Number.isInteger()` method determines whether the given value is an integer. It returns `true` if the value is an integer and `false` otherwise.


// Example of Number.isInteger()
console.log(Number.isInteger(42));         // true
console.log(Number.isInteger(42.5));       // false
console.log(Number.isInteger("42"));       // false
console.log(Number.isInteger(true));       // false

    

5. `Number.isFinite()` Method

The `Number.isFinite()` method determines whether the given value is a finite number. It returns `true` if the value is a finite number and `false` otherwise.


// Example of Number.isFinite()
console.log(Number.isFinite(1));           // true
console.log(Number.isFinite(Infinity));    // false
console.log(Number.isFinite(NaN));         // false
console.log(Number.isFinite("1"));         // false

    

6. `Number.isNaN()` Method

The `Number.isNaN()` method determines whether the given value is NaN (Not-a-Number). It returns `true` if the value is NaN and `false` otherwise.


// Example of Number.isNaN()
console.log(Number.isNaN(NaN));          // true
console.log(Number.isNaN(123));          // false
console.log(Number.isNaN("NaN"));        // false
console.log(Number.isNaN(undefined));    // false

    

7. `Number.parseFloat()` Method

The `Number.parseFloat()` method parses a string argument and returns a floating-point number. It is similar to the global `parseFloat()` function.


// Example of Number.parseFloat()
console.log(Number.parseFloat("3.14"));       // 3.14
console.log(Number.parseFloat("314e-2"));     // 3.14
console.log(Number.parseFloat("0.0314E+2"));  // 3.14
console.log(Number.parseFloat("3.14abc"));    // 3.14

    

8. `Number.parseInt()` Method

The `Number.parseInt()` method parses a string argument and returns an integer of the specified radix (base). It is similar to the global `parseInt()` function.


// Example of Number.parseInt()
console.log(Number.parseInt("42"));         // 42
console.log(Number.parseInt("101", 2));     // 5 (binary to decimal)
console.log(Number.parseInt("F", 16));      // 15 (hexadecimal to decimal)
console.log(Number.parseInt("42abc"));      // 42