JavaScript numbers are primitive data types, and unlike other programming languages, you don't need to declare different numeric types like int, float, or double. JavaScript numbers are always stored in double-precision 64-bit binary format (IEEE 754). This format stores numbers in 64 bits:
In JavaScript, numbers play an important role. Understanding their behavior is essential for effective programming. Let's explore the various aspects of numeric types.
JavaScript has only one numeric type: the double-precision 64-bit binary format (IEEE 754). This means it doesn't differentiate between integers and floating-point numbers explicitly.
JavaScript allows writing extra-large or extra-small numbers using scientific (exponent) notation.
// Extra-large numbers let a = 156e5;// Extra-small numbers let b = 156e-5;
console.log(a); // Output: 15600000 console.log(b); // Output: 0.00156
Integers (numbers without a period or exponent notation) are accurate up to 15 digits.
// 15-digit integer (accurate) let a = 999999999999999;// 16-digit integer (may lose precision) let b = 9999999999999999;
console.log(a); // Output: 999999999999999 console.log(b); // Output: 10000000000000000
Floating point arithmetic is not always 100% accurate due to binary representation limitations. To solve precision issues, multiply and divide by 10.
// Without solution (imprecise) let x = 0.22 + 0.12;// With solution (using multiply/divide) let y = (0.22 * 10 + 0.12 * 10) / 10;
console.log(x); // Output: 0.33999999999999997 console.log(y); // Output: 0.34
JavaScript uses the + operator for both addition and concatenation. Numbers are added; strings are concatenated.
// Adding two numbers let x = 10; let y = 15; let z = x + y; console.log(z); // Output: 25// Concatenating two strings let a = "10"; let b = "30"; let c = a + b; console.log(c); // Output: 1030
JavaScript automatically converts numeric strings to numbers in most arithmetic operations like division, multiplication, and subtraction.
// String division (auto-converts to numbers) let x = "100" / "10";// String multiplication let y = "100" * "10";
// String subtraction let z = "100" - "10";
console.log(x); // Output: 10 console.log(y); // Output: 1000 console.log(z); // Output: 90
You can use decimal, binary, octal, and hexadecimal number literals in JavaScript.
JavaScript has only one type of number and it can hold both integer and decimal values.
// Integer let a = 33;// Decimal let b = 3.3;
console.log(a); // Output: 33 console.log(b); // Output: 3.3
If a number starts with 0 and the following digits are smaller than 8, it is parsed as an Octal Number (base 8).
// Octal number (base 8) let x = 0562;console.log(x); // Output: 370 (converted to base 10)
Binary numbers start with 0b or 0B followed by 0's and 1's.
// Binary numbers let x = 0b11; let y = 0B0111;console.log(x); // Output: 3 console.log(y); // Output: 7
Hexadecimal numbers start with 0x or 0X followed by digits 0-9 and letters A-F.
// Hexadecimal number let x = 0xfff;console.log(x); // Output: 4095
Coercion refers to the automatic or implicit conversion of values from one data type to another. When different types of operators are applied, JavaScript performs type coercion.
When you perform an operation involving undefined, JavaScript returns NaN (Not-a-Number).
const res = undefined + 10; console.log(res); // Output: NaN
The value null is coerced to 0 when used in arithmetic operations.
const total = null + 5; console.log(total); // Output: 5
Boolean values (true and false) are converted to numbers: 1 for true and 0 for false.
const n1 = true + 10; const n2 = false + 10;console.log(n1); // Output: 11 console.log(n2); // Output: 10
When performing arithmetic operations, JavaScript converts strings to numbers. If the string cannot be parsed as a valid number, it returns NaN.
const s1 = '42'; const s2 = 'hello';const strToNum1 = Number(s1); const strToNum2 = Number(s2);
console.log(strToNum1); // Output: 42 console.log(strToNum2); // Output: NaN
JavaScript provides several useful number methods like toString(), toExponential(), toPrecision(), isInteger(), and toLocaleString().
let x = 21;console.log(x.toString()); // Output: "21" console.log(x.toExponential()); // Output: "2.1e+1" console.log(x.toPrecision(4)); // Output: "21.00" console.log(Number.isInteger(x)); // Output: true
new keyword0x are interpreted as hexadecimaltoString() to convert to bases 2-36BigInt numbers which are integers of arbitrary lengthWhat is the storage format for JavaScript numbers?