javascript

Number Methods


 

JavaScript has multiple built-in methods for working with numbers.

 

Some commonly used methods:

 

1. toFixed()

It converts a number to a string with rounding to a specified number of decimal places.

 

Example:

// toFixed()
let num = 15.335;
let result = num.toFixed(2); // Result will be "15.33"

 

2. toPrecision()

It converts a number to a string with rounding it to a specified number of significant digits.

 

Example:

// toPricision
let num = 123.456789;
let result = num.toPrecision(4); // Result will be "123.5"

 

3. toString()

It converts a number to a string.

 

Example:
 

// toString()
let num = 12;
let result = num.toString(); // Result will be "12"

 

4. parseInt()

It parses a string and returns an integer.

 

Example:
 

// parseInt()
let str = "12";
let result = parseInt(str); // Result will be 12

 

5. parseFloat()

It parses a string and returns a floating-point number.

 

Example:

// parseFloat()
let str = "1.23";
let result = parseFloat(str); // Result will be 1.23

 

6. IsNan()

It checks whether a value is Not-a-Number(NaN).

 

Example:

// isNan()
let num = 23;
let result = isNaN(num); // Result will be false