-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
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.
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.
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.
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.
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.
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.
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
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
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.
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.
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.
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));
let productPrice = 349.5;
console.log("Price: ₹" + productPrice.toFixed(2));
let inputAge = "22";
let age = Number(inputAge);
if (!isNaN(age)) {
console.log("Valid Age: " + age);
}
let heightText = "165cm";
let height = parseInt(heightText);
console.log("Height: " + height);
let result = 0.1 + 0.2;
console.log(result.toPrecision(3));
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()
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
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
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.
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?
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
