Here's something that trips up almost everyone coming from another language, JavaScript doesn't have separate types for integers and decimals. There's no int, no float, no double. Just one type called Number, and it handles all of it. That sounds simple, and mostly it is, but it also leads to a few genuinely weird moments that catch even experienced developers off guard.
Let's actually go through what this type can do, where it behaves oddly, and the handful of things worth knowing so those weird moments don't surprise you later.
Table of Contents
Everything Numeric Is Just a Number
let age = 25; let price = 19.99; let negative = -7; let scientific = 2.5e6;
console.log(typeof age);
output:-
number
console.log(typeof price);output:-
Notice both age and price come back as "number". There's no separate category for whole numbers versus decimals, it's all one type, and JavaScript figures out how to store it internally.number
The Classic 0.1 + 0.2 Surprise
This is the one that gets almost everyone at least once. Try this in your console:console.log(0.1 + 0.2);output:-
That's not a JavaScript bug, it's how computers store decimal numbers in general, using something called floating-point representation. Some decimals just can't be stored with perfect precision in binary, so tiny rounding errors creep in. It's rarely a problem for everyday use, but it becomes a real issue the moment you're comparing decimal values directly.0.30000000000000004
console.log(0.1 + 0.2 === 0.3);output:-
A safer way to compare decimals is checking if the difference is small enough to count as equal:false
function areClose(a, b) {
return Math.abs(a - b) < Number.EPSILON;
}
console.log(areClose(0.1 + 0.2, 0.3));
output:-
true
Special Numeric Values Worth Knowing
JavaScript has a few numeric values that don't behave like regular numbers, and it's worth recognizing them when they show up.console.log(1 / 0);output:-
Infinity
console.log(-1 / 0);output:-
-Infinity
console.log(0 / 0);output:-
NaN
console.log("abc" * 2);
output:-
NaN stands for "Not a Number," which is a little ironic since its type is actually "number". It shows up whenever a math operation doesn't produce a valid result. The tricky part is that NaN never equals itself.NaN
console.log(NaN === NaN); // false console.log(Number.isNaN(NaN)); // trueIf you ever need to check for NaN, always use Number.isNaN(), never a direct comparison.
Useful Number Methods You'll Actually Use
A few built-in tools make working with numbers a lot easier day to day.
console.log(Number.isInteger(10)); // true
console.log(Number.isInteger(10.5)); // false
console.log((3.14159).toFixed(2)); // "3.14"
console.log(parseInt("42px")); // 42
console.log(parseFloat("3.14 meters"));// 3.14
toFixed() is especially handy for anything involving money or display formatting, since it rounds to a fixed number of decimal places and returns a string. Just remember it returns a string, not a number, so keep that in mind if you need to do more math with the result afterward.There's a Limit to How Big a Number Can Safely Get
JavaScript numbers stay accurate up to a certain point, after which things get unreliable.console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992 console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992 (wrong!)Past that limit, JavaScript can't reliably tell numbers apart anymore. If you're dealing with genuinely huge numbers, like precise financial calculations or large IDs, there's a separate type called BigInt built specifically for that, written with an n at the end.
let big = 9007199254740991n + 2n; console.log(big); // 9007199254740993n
Conclusion
The Number type in JavaScript looks simple on the surface, and honestly, for most everyday code, it is. But the moment decimals, comparisons, or really large values get involved, a few of its quirks start to matter. Knowing about floating-point precision, NaN, and where the safe integer limit sits isn't about memorizing edge cases for fun, it's the difference between code that mostly works and code that doesn't quietly break on you six months from now.Frequently Asked Questions (FAQs)
1.Why doesn't JavaScript have separate integer and float types?2.Is NaN actually a number?It's a design choice from the language's early days. Everything numeric uses one internal format, which keeps things simple, though it's why quirks like the 0.1 + 0.2 issue show up.
3.When should I use BigInt instead of a regular number?Technically yes, typeof NaN returns "number", even though it represents an invalid numeric result. It's one of JavaScript's more confusing naming choices.
Only when you're working with values larger than Number.MAX_SAFE_INTEGER, like precise large IDs or financial totals where rounding errors genuinely can't be tolerated.
0 Comments