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 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?


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