Home 〉 Javascript Tutorials 〉 JavaScript Number and Number Methods Explained with Examples | Complete Guide
JavaScript Number and Number Methods Explained with Examples | Complete Guide !
Want to understand how numbers and number methods work in JavaScript? Numbers in JavaScript aren’t just digits—you can format, convert, and manipulate them using built-in methods. This tutorial explains key methods like toFixed(), toPrecision(), toString(), and more in simple language with examples. You’ll also learn how to convert strings into numbers using different approaches such as Number(), parseInt(), and parseFloat(). We’ve included step-by-step explanations and examples for each concept. Let’s dive into this complete guide on JavaScript Number and Number Methods.
In JavaScript, numbers can be integers or floating-point values. JavaScript provides the Number object to work with numerical values. The Number object comes with several built-in methods to handle, convert, and format numbers efficiently. These methods make numerical tasks simpler and more powerful for developers.
Below are the most commonly used JavaScript Number methods:
Each method is used with the following basic syntax:
html syntax ✍
numberObject.methodName(arguments)
Example 📄
let num = 123.456;
console.log(num.toFixed(2)); // Output: "123.46"
console.log(num.toPrecision(4)); // Output: "123.5"
console.log(num.toString()); // Output: "123.456"
console.log(num.valueOf()); // Output: 123.456
In the example above:
Example 📄
let n = 5.6789;
console.log(n.toFixed(2)); // "5.68"
toFixed() returns a string with the number rounded to the specified decimal places.
Example 📄
let n = 987.654;
console.log(n.toPrecision(5)); // "987.65"
toPrecision() formats a number to a specified length.
Example 📄
let n = 50;
console.log(n.toString()); // "50"
toString() converts a number to its string representation.
Example 📄
let n = new Number(45);
console.log(n.valueOf()); // 45
valueOf() returns the primitive numeric value from a Number object.
Example 📄
console.log(isNaN("abc")); // true
isNaN() checks whether a value is NaN (Not a Number).
Example 📄
console.log(isFinite(100)); // true
isFinite() checks if the number is finite.
Example 📄
let str = "123";
let num = Number(str);
console.log(num); // 123
The Number() function converts a string to a number directly.
Example 📄
let str = "123.45";
let num = parseInt(str);
console.log(num); // 123
parseInt() parses a string and returns an integer, ignoring decimals.
Example 📄
let str = "123.45";
let num = parseFloat(str);
console.log(num); // 123.45
parseFloat() keeps the decimal part of the number.
JavaScript provides a wide range of methods to manipulate numbers effectively. From formatting with toFixed() to converting strings using Number() and parseInt(), these built-in tools make working with numbers easier and more efficient. Practicing these methods will improve your control over data in any JavaScript application. Keep experimenting with each method for a deeper understanding.
parseInt() converts a string to an integer and ignores decimal points, while Number() converts the string to a number and retains decimal precision.
No, toFixed() returns a string representation of the number. You can convert it back using Number() if needed.
In most cases, JavaScript automatically uses valueOf() behind the scenes, but it’s useful when dealing with Number objects explicitly.
Yes, placing a plus sign before a string like +"123"
will convert it to a number.