HomeJavascript Tutorials 〉 JavaScript Number and Number Methods Explained with Examples | Complete Guide

JavaScript Number and Number Methods Explained with Examples | Complete Guide !

JavaScript Number and Number Methods Explained with Examples | Complete Guide !

Summary:

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.

Introduction to 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.

JavaScript Number Methods List

Below are the most commonly used JavaScript Number methods:

  • toFixed() - Formats a number using fixed-point notation.
  • toPrecision() - Formats a number to a specified length.
  • toString() - Converts a number to a string.
  • valueOf() - Returns the primitive value of a number object.
  • isNaN() - Checks if a value is NaN.
  • isFinite() - Checks if a number is a finite number.
  • parseInt() - Parses a string and returns an integer.
  • parseFloat() - Parses a string and returns a floating-point number.
  • Number() - Converts a value to a number.

Syntax of JavaScript Number Methods

Each method is used with the following basic syntax:

html syntax ✍

numberObject.methodName(arguments)

Example with Detailed Explanation

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:

  • toFixed(2) formats the number to two decimal places.
  • toPrecision(4) formats the number to 4 total digits.
  • toString() converts the number to a string format.
  • valueOf() returns the original number value.

JavaScript Number Methods with Detailed Examples

1. toFixed()

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.

2. toPrecision()

Example 📄

let n = 987.654;
console.log(n.toPrecision(5)); // "987.65"

toPrecision() formats a number to a specified length.

3. toString()

Example 📄

let n = 50;
console.log(n.toString()); // "50"

toString() converts a number to its string representation.

4. valueOf()

Example 📄

let n = new Number(45);
console.log(n.valueOf()); // 45

valueOf() returns the primitive numeric value from a Number object.

5. isNaN()

Example 📄

console.log(isNaN("abc")); // true

isNaN() checks whether a value is NaN (Not a Number).

6. isFinite()

Example 📄

console.log(isFinite(100)); // true

isFinite() checks if the number is finite.

Convert String to Number in JavaScript

1. Using Number()

Example 📄

let str = "123";
let num = Number(str);
console.log(num); // 123

The Number() function converts a string to a number directly.

2. Using parseInt()

Example 📄

let str = "123.45";
let num = parseInt(str);
console.log(num); // 123

parseInt() parses a string and returns an integer, ignoring decimals.

3. Using parseFloat()

Example 📄

let str = "123.45";
let num = parseFloat(str);
console.log(num); // 123.45

parseFloat() keeps the decimal part of the number.

Related Topics

  • For working with text, you may also want to check out our guide on JavaScript String and String Methods.
  • To know more about how JavaScript represents numbers, you can also visit the official IEEE 754 standard page used in JavaScript.

Conclusion

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.

Frequently Asked Questions (FAQs)

What is the difference between parseInt() and Number() in JavaScript?

parseInt() converts a string to an integer and ignores decimal points, while Number() converts the string to a number and retains decimal precision.

Can toFixed() return a number?

No, toFixed() returns a string representation of the number. You can convert it back using Number() if needed.

Is valueOf() needed often?

In most cases, JavaScript automatically uses valueOf() behind the scenes, but it’s useful when dealing with Number objects explicitly.

Can you convert string to number using the unary plus (+)?

Yes, placing a plus sign before a string like +"123" will convert it to a number.