The Art of Taking Control of Your Values (and Not Losing Your Mind) 🔢😄
Numbers in JavaScript…
One moment 2 + 2 = 4, the next moment 0.1 + 0.2 = 0.30000000000000004 😵
If you’ve ever asked yourself, “Does this language hate math?” — you’re not alone.
In this article, we will:
- Dive deep into Number methods
- Explain every piece of code step by step
- Show where things break and where they save you in real life
- Share mini tips and “a lot of people don’t know this” facts
If you’re ready, let’s grab the strings and control the numbers 🎮
1️⃣ What Is Number in JavaScript? (Everything in One Bag 🎒)
In JavaScript:
- Integer ❌
- Float ❌
- Double ❌
Everything is a single type: Number
let a = 10;
let b = 3.14;
let c = -100;
Explanation:
10→ Integer3.14→ Decimal-100→ Negative
👉 But JavaScript says:
“I treat all of these as Number.”
📌 Result:
This flexibility is sometimes convenient, sometimes pure chaos.
2️⃣ Number() – “Can I Turn You Into a Number?” 🤔
Number("42"); // 42
Number("42px"); // NaN
Number(true); // 1
Number(false); // 0
Number(""); // 0
Let’s Break It Down:
"42"→ Clean string → becomes a number ✅"42px"→ Contains letters → NaNtrue→ 1 (JavaScript logic)false→ 0""→ Empty, but JS says it’s 0 😅
🧠 Practical tip:
Everything coming from a form is a string.
If a user types 30, you don’t get 30 — you get "30".
3️⃣ What Is NaN? (Not a Number, but Its Type Is Number 🤯)
const result = Number("abc");
console.log(result); // NaN
The absurd part:
typeof NaN; // "number"
Yes…
It’s not a number, but its type is number 😂
To make peace with JavaScript, you just accept this.
4️⃣ parseInt() – “I’ll Take What I Can See” 👀
parseInt("42px"); // 42
parseInt("100.99"); // 100
How It Works:
- Starts from the left
- Reads numbers as long as it can
- Stops at the first non-number character
Radix (Very Important!)
parseInt("101", 2); // 5
👉 In binary, 101 equals 5
📌 If you don’t specify the radix, you’re taking a risk.
5️⃣ parseFloat() – Best Friend of Decimals 💙
parseFloat("19.99TL"); // 19.99
parseFloat("3.14abc"); // 3.14
What Does It Do?
- Understands decimal points
- Keeps the decimal part
- Stops at letters
📌 Golden Rule:
- Integer →
parseInt - Decimal →
parseFloat
6️⃣ isNaN() vs Number.isNaN() – Not the Same! ⚠️
isNaN("hello"); // true
Number.isNaN("hello"); // false
Why?
isNaN()→ converts to Number first, then checksNumber.isNaN()→ checks only real NaN values
✅ In modern JavaScript, always use:
Number.isNaN(value);
7️⃣ toFixed() – The Hero of Money Calculations 💰
const price = 19.9876;
price.toFixed(2); // "19.99"
🚨 Important Warning:
- The result is a string
const total = Number(price.toFixed(2));
📌 Real-life usage:
- Shopping carts
- Invoices
- Tax calculations
8️⃣ toPrecision() – Scientific Accuracy 🧪
const num = 123.456;
num.toPrecision(4); // "123.5"
What’s the Difference?
toFixed()→ digits after the decimaltoPrecision()→ total number of digits
📌 Commonly used in charts, measurements, and statistics.
9️⃣ The Math Object – The Gym of Numbers 🏋️♂️
The Rounding Trio
Math.round(4.5); // 5
Math.floor(4.9); // 4
Math.ceil(4.1); // 5
Explanation:
round→ nearest valuefloor→ round downceil→ round up
🔟 Random Numbers (But We’re in Control 🎲)
Math.random(); // Between 0 and 1
Between 1–10:
Math.floor(Math.random() * 10) + 1;
🎮 Games
🎁 Giveaways
🧪 Test data
1️⃣1️⃣ Safe Integers (Critical but Rarely Known 🧨)
Number.MAX_SAFE_INTEGER; // 9007199254740991
Number.isSafeInteger(9007199254740992); // false
👉 If you cross this limit:
The result works… but it’s wrong 😬
📌 Vital for finance and big data projects.
🔥 Real-Life Mini Recipes
Getting age from a form:
const age = Number(input.value);
if (Number.isNaN(age)) {
alert("Age must be a number!");
}
Money calculation:
const total = Number((price * tax).toFixed(2));
Safety check:
if (!Number.isSafeInteger(value)) {
console.log("Unsafe number!");
}
🎯 CONCLUSION: Making Peace with Numbers ✍️
Number Methods:
- Catch errors early
- Make your code more professional
- Reduce the “Why did this happen?” moments
If you work with numbers in JavaScript,
knowing Number methods is not optional — it’s mandatory.

