JavaScript Number Methods

๐Ÿ“˜ What are Number Methods?

Number methods are built-in functions used to work with numbers. In JavaScript, we use two dots . to call a method.

let num = 10.5;
num.toString();
            

๐Ÿ”น toString()

Converts a number to a string.

let num = 100;
let result = num.toString();
console.log(result); // "100"
            

๐Ÿ”น toFixed()

Rounds a number and keeps a fixed number of decimal places.

let num = 10.5678;
num.toFixed(2); // "10.57"
            
โš ๏ธ toFixed() returns a string, not a number.

๐Ÿ”น parseInt()

Extracts an integer from a string.

parseInt("100px"); // 100
parseInt("50.5");  // 50
            

๐Ÿ”น parseFloat()

Extracts a floating-point number from a string.

parseFloat("50.75px"); // 50.75
            

๐Ÿ”น Number.isInteger() (ES6)

Checks if a value is an integer.

Number.isInteger(10);   // true
Number.isInteger(10.5); // false
            

๐Ÿ”น Number.isNaN() (ES6)

Checks if a value is NaN (Not a Number).

Number.isNaN(NaN);    // true
Number.isNaN("abc"); // false
            

๐Ÿงช Try It Yourself

Click the button to see number methods in action: