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 Numbers


📘 JavaScript Numbers – Working with Numeric Data

In JavaScript, numbers are always stored as floating-point values (even whole numbers).

let x = 10;     // integer
let y = 10.5;   // floating-point

JavaScript does not differentiate between integers and floats like some other languages.


🔹 Number Types in JavaScript

Type Example
Integer 10, -25
Float (decimal) 3.14, -0.01
Exponential 5e35000
Hexadecimal 0xFF255
Octal 0o108
Binary 0b101010

🔹 Number Methods

Method Description Example
toString() Converts to string (123).toString()"123"
toFixed(n) Formats with n decimals (3.1415).toFixed(2)"3.14"
toPrecision(n) Formats to specified length (123.456).toPrecision(4)"123.5"
valueOf() Returns primitive number (123).valueOf()123
Number.isInteger(x) Checks if a value is an integer true or false
Number.isNaN(x) Checks if value is NaN true or false

🔸 Special Numeric Values

Value Description
Infinity A number beyond upper limit
-Infinity A number beyond lower limit
NaN "Not a Number"
let a = 1 / 0;     // Infinity
let b = "abc" - 2; // NaN

🔸 Type Conversion

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

📌 Number Limits

Number.MAX_VALUE     // Largest possible number
Number.MIN_VALUE     // Smallest positive number
Number.MAX_SAFE_INTEGER  // 2^53 - 1
Number.MIN_SAFE_INTEGER  // -(2^53 - 1)

Practice Questions

Q1. How do you declare a floating-point number 3.14 and store it in a variable pi?

Q2. How do you convert the string "45" into a number using the Number() function?

Q3. What is the output of (10).toFixed(2) and what does it represent?

Q4. How do you check if 25.0 is an integer using Number.isInteger()?

Q5. What is the result of typeof NaN and how is it interpreted in JavaScript?

Q6. How do you write the number 5000 using exponential notation?

Q7. How do you convert 255 into a hexadecimal string using toString()?

Q8. How do you find the largest number representable in JavaScript?

Q9. How do you check whether the result of "abc" - 5 is NaN?

Q10. How do you safely convert a decimal number like "3.14159" to a float?


JS Numbers Quiz

Q1: Which of the following is a valid number in JavaScript?

A. 10.5
B. 0x1A
C. 5e2
D. All of the above

Q2: What is the type of NaN in JavaScript?

A. "undefined"
B. "object"
C. "number"
D. "NaN"

Q3: What does (123.456).toFixed(1) return?

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

Q4: Which method checks if a number is an integer?

A. Number.check()
B. Number.isInteger()
C. isWhole()
D. isInt()

Q5: Which function would you use to convert "3.14" to a number?

A. parseInt()
B. String()
C. parseFloat()
D. Number.toFloat()

Q6: What is the result of 1 / 0 in JavaScript?

A. undefined
B. NaN
C. 0
D. Infinity

Q7: What does typeof Infinity return?

A. "undefined"
B. "number"
C. "Infinity"
D. "object"

Q8: Which of these values is not a finite number?

A. 100
B. 0
C. -9999
D. Infinity

Q9: Which property gives the maximum safe integer in JS?

A. Number.LIMIT_VALUE
B. Number.MAX
C. Number.MAX_SAFE_INTEGER
D. MAX.INTEGER

Q10: What does parseInt("45.67") return?

A. 45.67
B. NaN
C. 45
D. "45"

Go Back Top