Stop Letting JavaScript Numbers Fool You – Master Them in Minutes!
title: "Understanding Numbers in JavaScript: A Complete Guide" description: "From floating-point quirks to BigInt, learn how numbers really work in JavaScript with examples and best practices." tags: javascript, webdev, beginners, programming JavaScript is a powerful and flexible language, but its handling of numbers can be a bit quirky—especially if you're coming from languages like Python or Java. In this article, we’ll break down how numbers work in JavaScript — from basic arithmetic to edge cases you should know. What Is a Number in JavaScript? In JavaScript, all numbers are stored as floating-point values, even if they look like integers. console.log(typeof 10); // "number" console.log(typeof 10.5); // "number" JavaScript uses the IEEE 754 double-precision floating-point format. Basic Arithmetic let a = 5; let b = 2; console.log(a + b); // 7 console.log(a - b); // 3 console.log(a * b); // 10 console.log(a / b); // 2.5 console.log(a % b); // 1 (modulus) All standard math operations work as expected. Number Quirks to Watch Out For 1. Floating Point Precision console.log(0.1 + 0.2); // 0.30000000000000004 Due to how binary floating-point math works, some numbers can’t be represented exactly. 2. Comparing Floats console.log(0.1 + 0.2 === 0.3); // false Instead, compare using a small threshold: const epsilon = Number.EPSILON; console.log(Math.abs((0.1 + 0.2) - 0.3)

title: "Understanding Numbers in JavaScript: A Complete Guide"
description: "From floating-point quirks to BigInt, learn how numbers really work in JavaScript with examples and best practices."
tags: javascript, webdev, beginners, programming
JavaScript is a powerful and flexible language, but its handling of numbers can be a bit quirky—especially if you're coming from languages like Python or Java. In this article, we’ll break down how numbers work in JavaScript — from basic arithmetic to edge cases you should know.
What Is a Number in JavaScript?
In JavaScript, all numbers are stored as floating-point values, even if they look like integers.
console.log(typeof 10); // "number"
console.log(typeof 10.5); // "number"
JavaScript uses the IEEE 754 double-precision floating-point format.
Basic Arithmetic
let a = 5;
let b = 2;
console.log(a + b); // 7
console.log(a - b); // 3
console.log(a * b); // 10
console.log(a / b); // 2.5
console.log(a % b); // 1 (modulus)
All standard math operations work as expected.
Number Quirks to Watch Out For
1. Floating Point Precision
console.log(0.1 + 0.2); // 0.30000000000000004
Due to how binary floating-point math works, some numbers can’t be represented exactly.
2. Comparing Floats
console.log(0.1 + 0.2 === 0.3); // false
Instead, compare using a small threshold:
const epsilon = Number.EPSILON;
console.log(Math.abs((0.1 + 0.2) - 0.3) < epsilon); // true
Special Number Values
JavaScript includes some special number values:
Infinity
-Infinity
-
NaN
(Not a Number)
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
title: "Understanding Numbers in JavaScript: A Complete Guide"
description: "From floating-point quirks to BigInt, learn how numbers really work in JavaScript with examples and best practices."
tags: javascript, webdev, beginners, programming
JavaScript is a powerful and flexible language, but its handling of numbers can be a bit quirky—especially if you're coming from languages like Python or Java. In this article, we’ll break down how numbers work in JavaScript — from basic arithmetic to edge cases you should know.
---
## What Is a Number in JavaScript?
In JavaScript, **all numbers are stored as floating-point values**, even if they look like integers.
js
console.log(typeof 10); // "number"
console.log(typeof 10.5); // "number"
JavaScript uses the **IEEE 754 double-precision** floating-point format.
---
## Basic Arithmetic
js
let a = 5;
let b = 2;
console.log(a + b); // 7
console.log(a - b); // 3
console.log(a * b); // 10
console.log(a / b); // 2.5
console.log(a % b); // 1 (modulus)
All standard math operations work as expected.
---
## Number Quirks to Watch Out For
### 1. Floating Point Precision
js
console.log(0.1 + 0.2); // 0.30000000000000004
Due to how binary floating-point math works, some numbers can’t be represented exactly.
### 2. Comparing Floats
js
console.log(0.1 + 0.2 === 0.3); // false
Instead, compare using a small threshold:
js
const epsilon = Number.EPSILON;
console.log(Math.abs((0.1 + 0.2) - 0.3) < epsilon); // true
---
## Special Number Values
JavaScript includes some special number values:
* `Infinity`
* `-Infinity`
* `NaN` (Not a Number)
js
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
description: "From floating-point quirks to BigInt, learn how numbers really work in JavaScript with examples and best practices."
tags: javascript, webdev, beginners, programming
JavaScript is a powerful and flexible language, but its handling of numbers can be a bit quirky—especially if you're coming from languages like Python or Java. In this article, we’ll break down how numbers work in JavaScript — from basic arithmetic to edge cases you should know.
What Is a Number in JavaScript?
In JavaScript, all numbers are stored as floating-point values, even if they look like integers.
console.log(typeof 10); // "number"
console.log(typeof 10.5); // "number"
JavaScript uses the IEEE 754 double-precision floating-point format.
Basic Arithmetic
let a = 5;
let b = 2;
console.log(a + b); // 7
console.log(a - b); // 3
console.log(a * b); // 10
console.log(a / b); // 2.5
console.log(a % b); // 1 (modulus)
All standard math operations work as expected.
Number Quirks to Watch Out For
1. Floating Point Precision
console.log(0.1 + 0.2); // 0.30000000000000004
Due to how binary floating-point math works, some numbers can’t be represented exactly.
2. Comparing Floats
console.log(0.1 + 0.2 === 0.3); // false
Instead, compare using a small threshold:
const epsilon = Number.EPSILON;
console.log(Math.abs((0.1 + 0.2) - 0.3) < epsilon); // true
Special Number Values
JavaScript includes some special number values:
Infinity
-Infinity
-
NaN
(Not a Number)
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
Source code : https://github.com/shifa-23/JS-vault/blob/main/js_numbers