Skip to main content

JavaScript Numbers

By SamK
0
0 recommends
Category(s)
Topic(s)

JavaScript uses a single type for representing numbers, which covers both integers and floating-point values. This means numbers can be expressed with or without decimal points.

const pi = 3.14;
const wholeNumber = 3;
console.log(`Value of Pi: ${pi}`); //Value of Pi: 3.14
console.log(`Whole Number: ${wholeNumber}`); //Whole Number: 3

JavaScript supports scientific notation for representing very large or very small numbers.

const largeNumber = 1.23e7; // 12300000
const smallNumber = 1.23e-3; // 0.00123
console.log(`Large Number (Scientific Notation): ${largeNumber}`); //Large Number (Scientific Notation): 12300000
console.log(`Small Number (Scientific Notation): ${smallNumber}`); //Small Number (Scientific Notation): 0.00123

64-bit Floating Point Representation

JavaScript numbers follow the IEEE 754 standard:

  • Bits 0 to 51: Stores the fraction (mantissa)
  • Bits 52 to 62: Stores the exponent
  • Bit 63: Stores the sign (0 for positive, 1 for negative)

Integer Precision

JavaScript accurately maintains precision for whole numbers up to 15 digits.

const preciseNumber = 999_999_999_999_999;
const impreciseNumber = 10_000_000_000_000_000;
console.log(`Accurate Number: ${preciseNumber}`); //Accurate Number: 999999999999999
console.log(`Inaccurate Number: ${impreciseNumber}`); //Inaccurate Number: 10000000000000000

Floating-Point Precision Issue

Floating-point calculations may introduce small inaccuracies.

const sum = (0.2 + 0.1).toFixed(2);
console.log(`Result of 0.2 + 0.1: ${sum}`); //Result of 0.2 + 0.1: 0.30

To correct precision errors:

const correctedSum = ((0.2 * 10) + (0.1 * 10)) / 10;
console.log(`Corrected calculation: ${correctedSum.toFixed(1)}`); //Corrected calculation: 0.3

Adding Numbers and Strings

JavaScript’s + operator serves both as an addition and a string concatenation operator.

const firstNumber = 10;
const secondNumber = 20;
console.log(`The result is: ${firstNumber + secondNumber}`); //The result is: 30

When two strings are added, they concatenate:

const firstString = "10";
const secondString = "20";
console.log(`The combined result is: ${firstString + secondString}`); //The combined result is: 1020

Mixing numbers and strings results in concatenation:

const numberValue = 10;
const stringValue = "20";
console.log(`Concatenated result: ${numberValue + stringValue}`); //Concatenated result: 1020

JavaScript will convert numeric strings to numbers in mathematical operations.

let x = "100";
let y = "10";
console.log(Number(x) / Number(y)); //10
console.log(Number(x) * Number(y)); //1000
console.log(Number(x) - Number(y)); //90

NaN - Not a Number

NaN represents an invalid number operation.

console.log(100 / "Apple"); // NaN
console.log(isNaN(100 / "Apple")); // true

Infinity

A number divided by zero results in Infinity.

console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity

Hexadecimal, Octal, and Binary Representations

Numbers prefixed with 0x are hexadecimal. The toString() method can convert numbers into different bases.

const num = 32;
console.log(`Base 16: ${num.toString(16)}`); //Base 16: 20
console.log(`Base 8: ${num.toString(8)}`); //Base 8: 40
console.log(`Base 2: ${num.toString(2)}`); //Base 2: 100000

Numbers as Objects

Numbers are usually primitive values but can also be objects.

const primitiveValue = 150;
const numberObject = new Number(150);
console.log(`Type of primitiveValue: ${typeof primitiveValue}`); //Type of primitiveValue: number
console.log(`Type of numberObject: ${typeof numberObject}`); //Type of numberObject: object

Avoid using new Number(), as it introduces unnecessary complexity.

Comparing Numbers and Objects

Using == compares values, while === compares values and types.

let a = 200;
let b = new Number(200);
console.log(a == b); // true
console.log(a === b); // false

Two number objects are always different when compared.

let x = new Number(500);
let y = new Number(500);
console.log(x == y); // false
console.log(x === y); // false

JavaScript treats objects as different instances, even if they contain the same value.

Questions & Answers