JavaScript

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

JavaScript

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 Number Methods – Formatting and Processing Numbers

JavaScript provides several built-in methods that can be used on number values. These methods help with tasks such as formatting decimals, converting types, checking values, and more.

let num = 123.456;

🔹 1. toString()

Converts a number to a string.

(123).toString();       // "123"
(255).toString(16);     // "ff" (hexadecimal)

🔹 2. toFixed(n)

Formats a number using fixed-point notation.

let n = 5.6789;
n.toFixed(2);           // "5.68"

Returns a string with n digits after the decimal.


🔹 3. toPrecision(n)

Formats a number to n total digits (includes digits before and after decimal).

let x = 123.456;
x.toPrecision(4);       // "123.5"

🔹 4. valueOf()

Returns the primitive value of a number (often used internally).

let x = new Number(50);
x.valueOf();            // 50

🔹 5. Number.isInteger(value)

Checks if a value is an integer.

Number.isInteger(10);   // true
Number.isInteger(10.5); // false

🔹 6. Number.isNaN(value)

Checks if a value is exactly NaN (Not a Number).

Number.isNaN(NaN);        // true
Number.isNaN("hello");    // false
Number.isNaN(Number("abc")); // true

🔹 7. parseInt() and parseFloat()

Used to convert strings to numbers.

parseInt("123.45");     // 123
parseFloat("123.45");   // 123.45

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?


JS Number Methods Quiz

Q1: What does (255).toString(16) return?

A. "255"
B. "ff"
C. "0xff"
D. "f5"

Q2: Which method formats a number to a specific number of decimal places?

A. toFixed()
B. toPrecision()
C. valueOf()
D. toString()

Q3: What is the output of parseInt("123.456")?

A. 123.456
B. 123
C. NaN
D. "123"

Q4: What does Number.isInteger(10.0) return?

A. false
B. true
C. NaN
D. "true"

Q5: Which method checks if a value is exactly NaN?

A. isNaN()
B. Number.isNaN()
C. typeof NaN
D. checkNaN()

Q6: What is the result of (123.456).toPrecision(5)?

A. "123.456"
B. "123.46"
C. "123.5"
D. "123.46"

Q7: Which method returns the primitive value of a number object?

A. value()
B. getValue()
C. valueOf()
D. primitive()

Q8: What is the output of Number.isNaN("abc")?

A. true
B. false
C. undefined
D. "false"

Q9: Which method would you use to get the value 123.45 from the string "123.45abc"?

A. Number()
B. parseInt()
C. parseFloat()
D. toFixed()

Q10: What will (5.6789).toFixed(2) return?

A. 5.67
B. "5.6789"
C. "5.68"
D. 5.68

Go Back Top