JavaScript

coding learning websites codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Number Methods


JavaScript numbers are not just simple values used for calculations. They also come with a rich set of built-in methods that help you format numbers, control decimal precision, convert values, and perform safe numeric checks. These methods belong to the Number object and are extremely useful in real-world applications such as financial calculations, score systems, reports, dashboards, and user interfaces.

In this chapter, you will learn what JavaScript number methods are, why they are important, how the most commonly used number methods work, practical examples, common mistakes, best practices, and real-world use cases.

What Are Number Methods

Number methods are built-in functions provided by JavaScript that can be used on numeric values to perform specific operations. These methods help you convert numbers into strings, control decimal places, check whether a value is valid, and format output properly.

Number methods do not change the original number. Instead, they return a new value based on the operation.

let score = 88.456;
let roundedScore = score.toFixed(2);

Here, score remains unchanged, and roundedScore receives the formatted value.

Why Number Methods Are Important

Number methods help you:

  • Format numbers for display

  • Control decimal precision

  • Convert numbers into strings

  • Validate numeric values

  • Prevent calculation errors

  • Improve readability of output

Without number methods, handling numeric data accurately would become difficult and error-prone.

Commonly Used JavaScript Number Methods

toString()

The toString() method converts a number into a string.

let age = 24;
let ageText = age.toString();

console.log(ageText);
console.log(typeof ageText);

This is useful when displaying numbers as text or concatenating them with strings.

toFixed()

The toFixed() method formats a number using a fixed number of decimal places.

let price = 199.9876;
let formattedPrice = price.toFixed(2);

console.log(formattedPrice); // "199.99"

The result is always a string.

This method is commonly used in currency and percentage values.

toPrecision()

The toPrecision() method formats a number to a specified total number of digits.

let value = 123.4567;

console.log(value.toPrecision(4)); // "123.5"
console.log(value.toPrecision(6)); // "123.457"

This method is useful when you want overall precision rather than decimal control.

Number()

The Number() method converts a value into a number.

let inputMarks = "85";
let marks = Number(inputMarks);

console.log(marks);
console.log(typeof marks);

If conversion fails, the result becomes NaN.

let invalidValue = Number("Ananya");
console.log(invalidValue); // NaN

parseInt()

The parseInt() method converts a string into an integer.

let ageText = "21 years";
let age = parseInt(ageText);

console.log(age); // 21

You can also specify the base.

let binaryValue = "1010";
let decimalValue = parseInt(binaryValue, 2);

console.log(decimalValue); // 10

parseFloat()

The parseFloat() method converts a string into a floating-point number.

let percentageText = "87.5%";
let percentage = parseFloat(percentageText);

console.log(percentage); // 87.5

This is helpful when working with decimal values from user input.

isNaN()

The isNaN() method checks whether a value is Not a Number.

let score = 90;
let name = "Riya";

console.log(isNaN(score)); // false
console.log(isNaN(name));  // true

This method helps validate numeric input.

isFinite()

The isFinite() method checks whether a value is a finite number.

console.log(isFinite(100));      // true
console.log(isFinite(Infinity)); // false
console.log(isFinite(NaN));      // false

This is useful when handling divisions or calculations that may result in Infinity.

Practical Examples

Example 1: Student Average with Formatting

let marksAditi = 78.456;
let marksBhavya = 85.234;
let marksKavya = 91.876;

let average = (marksAditi + marksBhavya + marksKavya) / 3;
console.log("Average Marks: " + average.toFixed(2));

Example 2: Product Price Display

let productPrice = 349.5;
console.log("Price: ₹" + productPrice.toFixed(2));

Example 3: Converting User Input

let inputAge = "22";
let age = Number(inputAge);

if (!isNaN(age)) {
    console.log("Valid Age: " + age);
}

Example 4: Extracting Numeric Value

let heightText = "165cm";
let height = parseInt(heightText);

console.log("Height: " + height);

Example 5: Precision Control

let result = 0.1 + 0.2;
console.log(result.toPrecision(3));

Common Mistakes

  • Assuming toFixed() returns a number instead of a string

  • Forgetting to validate numeric input

  • Using parseInt() for decimal values

  • Ignoring NaN results

  • Confusing toFixed() and toPrecision()

Best Practices

  • Use toFixed() for currency and percentages

  • Validate input using isNaN() before calculations

  • Use parseFloat() for decimal input

  • Convert values explicitly instead of relying on automatic conversion

  • Keep formatting logic separate from calculations

Real-World Applications

JavaScript number methods are widely used in:

  • E-commerce price calculations

  • Financial reports and dashboards

  • Student grading systems

  • Analytics and data visualization

  • Form input validation

Summary of JS Number Methods

JavaScript number methods provide powerful tools to format, convert, and validate numeric values. Methods like toFixed(), toPrecision(), parseInt(), and parseFloat() help control how numbers are displayed and processed. By understanding and applying these methods correctly, you can handle numeric data accurately, avoid common errors, and build reliable, user-friendly JavaScript applications.


Practice Questions

Q1. How do you convert the number 150 into a string using toString()?

Q2. How do you format the number 3.14159 to two decimal places using toFixed()?

Q3. How do you use toPrecision() to format the number 456.789 to 4 significant digits?

Q4. What is the result of valueOf() when used on new Number(75)?

Q5. How do you check if a number 20.5 is an integer using Number.isInteger()?

Q6. How do you check if a variable x = "abc" is NaN after converting with Number()?

Q7. How do you parse the string "55.99" into a whole number using parseInt()?

Q8. How do you parse the string "55.99" into a floating-point number using parseFloat()?

Q9. How do you convert the number 255 into a binary string using toString()?

Q10. What is the difference between toFixed() and toPrecision() with an example?


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

JavaScript

online coding class codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top