Javascript Number Methods

Rahul Kaklotar
3 min readApr 7, 2023

--

Javascript Number Methods

JavaScript is a programming language used to create interactive web pages and web applications. One of the data types in JavaScript is numbers, which can be manipulated using various built-in methods. In this article, we will discuss some of the most commonly used JavaScript number methods along with examples.

1. toFixed():

This method is used to format a number with a specified number of decimal places. It returns a string representation of the number. For example:

let num = 10.567;
console.log(num.toFixed(2)); // Output: 10.57
console.log(num.toFixed(0)); // Output: 11
Javascript Number Methods

2. toPrecision():

This method is used to format a number with a specified number of significant digits. It returns a string representation of the number. For example:

let num = 123.456;
console.log(num.toPrecision(3)); // Output: 123
console.log(num.toPrecision(5)); // Output: 123.46
Javascript Number Methods

3. parseInt():

This method is used to convert a string to an integer. It takes a string as an argument and returns an integer. For example:

let str = "10";
console.log(parseInt(str)); // Output: 10
Javascript Number Methods

4. parseFloat():

This method is used to convert a string to a floating-point number. It takes a string as an argument and returns a floating-point number. For example:

let str = "10.5";
console.log(parseFloat(str)); // Output: 10.5
Javascript Number Methods

5. isNaN():

This method is used to determine whether a value is NaN (Not a Number). It takes a value as an argument and returns a Boolean value. For example:

console.log(isNaN(10)); // Output: false
console.log(isNaN("10")); // Output: false
console.log(isNaN("hello")); // Output: true
Javascript Number Methods

6. Math.floor():

This method is used to round a number down to the nearest integer. It takes a number as an argument and returns an integer. For example:

let num = 10.8;
console.log(Math.floor(num)); // Output: 10
Javascript Number Methods

7. Math.ceil():

This method is used to round a number up to the nearest integer. It takes a number as an argument and returns an integer. For example:

let num = 10.2;
console.log(Math.ceil(num)); // Output: 11

In conclusion, JavaScript provides many built-in methods for manipulating numbers. The above-mentioned methods are just a few examples of the many methods available in JavaScript. By understanding and utilizing these methods, you can make your JavaScript code more efficient and effective.

--

--

Rahul Kaklotar

I'm front-end developer with 4 years of expertise in HTML, CSS, JavaScript, React. Passionate about creating user-friendly websites with a sharp eye for detail.