Number Properties in JavaScript

Number Properties in JavaScript

Number Properties (A Guide That Will Make You Rethink What You Know 🚀)

If you hear someone say “Numbers are simple in JavaScript,” chances are they haven’t spent quality time alone with NaN yet…

In JavaScript, numbers seem to belong to a single type (number).
But behind that single type, there are limits, traps, safe zones, and full-blown mathematical dramas.

This article is here to turn those dramas into comedy 🎭


Let’s Clarify This First 🎯

let age = 25;

This 25 is a primitive number.
But Number is the constitution book where JavaScript stores all the rules about numbers 📘

And now, we’re diving into the most critical articles of that constitution.


1️⃣ Number.MAX_VALUE

“The largest number JavaScript can handle”

console.log(Number.MAX_VALUE);
// 1.7976931348623157e+308

This number basically says:

“I work up to here. Beyond this point, I’m out.”

What happens if we exceed it?

let result = Number.MAX_VALUE * 2;
console.log(result);
// Infinity

💥 Boom! The number shoots straight into infinity.

Why Is This Important?

  • Large financial calculations
  • Scientific computations
  • Huge numbers coming from APIs

Practical Tip 💡

if (value > Number.MAX_VALUE) {
  console.log("This number is too large for JavaScript!");
}


2️⃣ Number.MIN_VALUE

“The closest positive number to zero (but not zero)”

console.log(Number.MIN_VALUE);
// 5e-324

⚠️ Very common mistake:

“This is the smallest negative number”

❌ No
✅ This is the smallest positive number greater than zero

Example

let tiny = Number.MIN_VALUE / 2;
console.log(tiny);
// 0

JavaScript basically says:

“I’ll treat this as zero now, sorry.”

Where Is It Useful?

  • Precision-sensitive calculations
  • Scientific measurements
  • Testing floating-point limits

3️⃣ Infinity, POSITIVE_INFINITY, NEGATIVE_INFINITY

“Numbers going to space”

console.log(Number.POSITIVE_INFINITY); // Infinity
console.log(Number.NEGATIVE_INFINITY); // -Infinity

How do they occur?

console.log(1 / 0);   // Infinity
console.log(-1 / 0);  // -Infinity

JavaScript doesn’t throw an error here. It just says:

“Okay, this is infinity now.”

How to Check It

if (value === Infinity) {
  console.log("We’ve reached infinity 🚀");
}

Practical Uses

  • Mathematical limits
  • Detecting runaway calculations
  • Graphics and animation logic

4️⃣ Number.NaN

“Not a number, but JavaScript refuses to give up”

let result = 10 / "banana";
console.log(result);
// NaN

NaN = Not a Number

But here’s where it gets interesting 👇

The Biggest Trap 😈

NaN === NaN // false

Yes…
NaN is not even equal to itself.

✅ The CORRECT Way to Check

Number.isNaN(result); // true

❌ The Wrong Way

isNaN("123"); // false, but confusing

Practical Tip 💡

Always use Number.isNaN() — your sanity will thank you.


5️⃣ Number.EPSILON

“The mediator of decimal numbers”

We’ve all seen this scene before:

0.1 + 0.2 === 0.3
// false 😤

Why?

JavaScript stores decimal numbers using binary representation.

Here’s the solution 👇

function isEqual(a, b) {
  return Math.abs(a - b) < Number.EPSILON;
}

isEqual(0.1 + 0.2, 0.3); // true 🎉

What Did We Do?

  • Calculated the difference between two numbers
  • If the difference is smaller than EPSILON, we treat them as equal

Where Is This Critical?

  • Money calculations
  • Measurement systems
  • Graphics and animation math

6️⃣ Number.MAX_SAFE_INTEGER

“The last integer JavaScript can count safely”

console.log(Number.MAX_SAFE_INTEGER);
// 9007199254740991

Danger Zone 🚨

let x = 9007199254740992;
console.log(x + 1 === x);
// true 😱

At this point, JavaScript starts losing track.

Why?

Because precision is lost beyond this limit.

The Solution 🛠️

let big = 9007199254740992n;
console.log(big + 1n);
// 9007199254740993n

👉 BigInt to the rescue.


7️⃣ Number.MIN_SAFE_INTEGER

“The safe boundary on the negative side”

console.log(Number.MIN_SAFE_INTEGER);
// -9007199254740991

Same rules apply here.

Practical Check

function isSafe(num) {
  return Number.isSafeInteger(num);
}

isSafe(123); // true
isSafe(9007199254740992); // false


Real-Life Mini Scenario 🎬

let price = 0.1 + 0.2;

if (Math.abs(price - 0.3) < Number.EPSILON) {
  console.log("The price is correct 💸");
}

👉 If you’re building a financial application, this code can save your life.


Save-Worthy Super Summary 📌

  • Big numbers → MAX_VALUE
  • Safe integers → MAX_SAFE_INTEGER
  • Decimal battles → EPSILON
  • Weird results → NaN
  • Infinity → Infinity

Final Words ☕

Using numbers in JavaScript is easy.
Using them correctly requires knowledge.

After reading this article:

  • You won’t ask “Why is this happening?”
  • NaN won’t scare you anymore
  • You’ll make peace with decimal numbers ✌️

Comments

No comments yet. Why don’t you start the discussion?

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir