🔢 Number Data Type
Last Updated: 7th October 2025
The Number is used to represent numeric values — both integers and decimals.
Hinglish Tip 🗣: Jaise maths me numbers hote hai (1, 2.5, -3), waise hi JS me bhi hum unhe variables me store kar sakte hain.
Declaring Numbers
You can declare numbers using let, const, or var.
let x = 10; // Integer
let y = 3.14; // Decimal (Float)
let z = -25; // Negative number
Special Number Values
| Value | Meaning | Example |
|---|---|---|
| InfiniTy | PosiTive infiniTy | 1 / 0 → InfiniTy |
| -InfiniTy | NegaTive infiniTy | -1 / 0 → -InfiniTy |
| NaN | NoT a Number | "abc" / 5 → NaN |
| BigInT | Large inTeger values (-2^53-1 to 2^53-1) | 12345678901234567890n |
Note: Agar koi value number me convert nahi ho sakti, toh result aata hai NaN.
Example
let a = 42;
console.log(typeof a); // "number"
console.log(typeof 3.14); // "number"
console.log(typeof NaN); // "number"
console.log(typeof Infinity); // "number"
let big = 123456789012345678901234567890n;
console.log(typeof big); // "bigint"
Note 🧠: In JS, NaN ka matlab “Not a Number” hota hai, par fir bhi iska type "number" hota hai 😅
Round a Number
JavaScript provides several methods to work with numbers easily — like rounding, converting strings to numbers, and checking validity.
Hinglish Tip 🗣: Ye methods help karte hain jab hume number ke sath calculation, rounding, ya type conversion karni hoti hai.
1. toFixed()
- Rounds a number to a fixed number of decimal places.
- Returns a string value.
let a = 9.8765;
console.log(a.toFixed(2)); // "9.88"
2. toPrecision()
- Formats a number to a specified total number of digits.
- Returns a string value.
let b = 123.456;
console.log(b.toPrecision(4)); // "123.5"