JavaScript Numbers
In JavaScript, all numbers — whether integers like 10 or decimals like 10.5—are represented internally as 64-bit floating-point numbers. JavaScript does not have a separate integer type for standard numbers; under the hood, 10 is stored identically to 10.0.
Example 1: (Integer vs Decimal)
console.log(10 === 10.0);
// Output: trueHere JavaScript stores both 10 and 10.0 as the exact same double-precision binary floating-point value, strict equality (===) returns true.
Example 2: (The Floating-Point Precision Trap)
console.log(0.1 + 0.2);
// Output: 0.30000000000000004Humans write numbers in decimal (base-10), but computers store and process them in binary (base-2). Just like 1/3 cannot be represented cleanly in base-10 (0.3333…), fractions like 0.1 and 0.2 turn into repeating infinite fractions in base-2. When added together, the tiny rounding errors compound into 0.30000000000000004.
Example 3: Comparing Floating-Point Calculations
console.log(0.3 === 0.1 + 0.2);
// Output: falseHere 0.1 + 0.2 evaluates to 0.30000000000000004, comparing it directly to 0.3 fails.0.1 + 0.2 evaluates to 0.30000000000000004, comparing it directly to 0.3 fails.
Example 4:
console.log(0.3 === Number((0.1 + 0.2).toFixed(1)));
// Output: trueHere the toFixed() method formats a number using fixed-point notation, but it returns a string.
In console.log(0.3 === (Number(0.1 + 0.2).toFixed(1))), Number(...) runs before .toFixed(1). Because .toFixed() returns a string, comparing 0.3 (a number) to "0.3" (a string) with === returns false.
Convert Strings to Numbers :
- Using the Number() Function:
const str = “12”;
const num = Number(str);
//Output: 122. Using the Unary Plus (+) Operator
Get Aaranya’s stories in your inbox
Join Medium for free to get updates from this writer.
The + unary operator acts as a concise shorthand for Number()
const str = “12”;
const num = +str;
// Output:123. ParseInt
Parses the string from left to right until it hits a non-numeric character. It discards everything after the invalid character and truncates decimals to return an integer.
const str = Number.parseInt(‘30px’, 10);
//Output:30JavaScript looks at the text ‘30px’ , extracts the number 30, and ignores the ‘px’.
Key Rules to Remember:
Rule A: It stops at the FIRST non-digit character
Number.parseInt(‘100.50px’, 10);
// Returns 100 (Stops at the decimal point ‘.’)
Number.parseInt(’42 percent’, 10);
// Returns 42 (Stops at the space)Rule B: Leading whitespace is ignored
If your string starts with spaces, JavaScript skips them automatically until it finds a character to evaluate.
Number.parseInt(‘ 99’, 10);
// Returns 99Rule C: It MUST start with a valid digit or sign
If the very first non-space character is not a number or a sign (+ or -), JavaScript gives up immediately and returns NaN(Not a Number).
Number.parseInt(‘px30’, 10);
// Returns NaN (First character ‘p’ is not a digit)
Number.parseInt('$50', 10);
// Returns NaN (First character '$' is not a digit)4.ParseFloat
Number.parseInt(‘30.10’, 10);
// Returns 30
Number.parseFloat('30.10');
// Returns 30.1parseFloat converts the text into a raw mathematical number. In mathematics, trailing zeros after a decimal point (like the 0 at the end of 30.10) have no numerical value, so JavaScript drops them automatically when printing the number.
Notes:
- Every standard number in JS is a double-precision floating-point number.
- Avoid comparing floating-point additions directly using
===. - Use
.toFixed()for display or comparison, but remember to convert the string output back to a number before strict type checking.








