JS Numbers

JavaScript Numbers

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:


Numeric Types in JavaScript

In JavaScript, numbers play an important role. Understanding their behavior is essential for effective programming. Let's explore the various aspects of numeric types.

The Only Numeric Type

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.


Scientific Notation

JavaScript allows writing extra-large or extra-small numbers using scientific (exponent) notation.

Scientific Notation Example

// Extra-large numbers
let a = 156e5;

// Extra-small numbers let b = 156e-5;

console.log(a); // Output: 15600000 console.log(b); // Output: 0.00156


Integer Precision

Integers (numbers without a period or exponent notation) are accurate up to 15 digits.

Integer Precision Example

// 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 Precision

Floating point arithmetic is not always 100% accurate due to binary representation limitations. To solve precision issues, multiply and divide by 10.

Floating Point Precision Example

// 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


Adding Numbers and Strings

JavaScript uses the + operator for both addition and concatenation. Numbers are added; strings are concatenated.

Adding Numbers vs Strings Example

// 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


Numeric Strings

JavaScript automatically converts numeric strings to numbers in most arithmetic operations like division, multiplication, and subtraction.

Numeric Strings Example

// 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


Number Literals

You can use decimal, binary, octal, and hexadecimal number literals in JavaScript.

Decimal Numbers

JavaScript has only one type of number and it can hold both integer and decimal values.

Decimal Numbers Example

// Integer
let a = 33;

// Decimal let b = 3.3;

console.log(a); // Output: 33 console.log(b); // Output: 3.3

Octal Numbers

If a number starts with 0 and the following digits are smaller than 8, it is parsed as an Octal Number (base 8).

Octal Numbers Example

// Octal number (base 8)
let x = 0562;

console.log(x); // Output: 370 (converted to base 10)

Binary Numbers

Binary numbers start with 0b or 0B followed by 0's and 1's.

Binary Numbers Example

// Binary numbers
let x = 0b11;
let y = 0B0111;

console.log(x); // Output: 3 console.log(y); // Output: 7

Hexadecimal Numbers

Hexadecimal numbers start with 0x or 0X followed by digits 0-9 and letters A-F.

Hexadecimal Numbers Example

// Hexadecimal number
let x = 0xfff;

console.log(x); // Output: 4095


Number Coercion in JavaScript

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.

Undefined to NaN

When you perform an operation involving undefined, JavaScript returns NaN (Not-a-Number).

Undefined to NaN Example

const res = undefined + 10;
console.log(res); // Output: NaN

Null to 0

The value null is coerced to 0 when used in arithmetic operations.

Null to 0 Example

const total = null + 5;
console.log(total); // Output: 5

Boolean to Number

Boolean values (true and false) are converted to numbers: 1 for true and 0 for false.

Boolean to Number Example

const n1 = true + 10;
const n2 = false + 10;

console.log(n1); // Output: 11 console.log(n2); // Output: 10

String to Number

When performing arithmetic operations, JavaScript converts strings to numbers. If the string cannot be parsed as a valid number, it returns NaN.

String to Number Example

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 Number Methods

JavaScript provides several useful number methods like toString(), toExponential(), toPrecision(), isInteger(), and toLocaleString().

Number Methods Example

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


Key Facts About JavaScript Numbers

Question 1 ?

What is the storage format for JavaScript numbers?