Numbers & Math

JavaScript uses the Number type to represent both integer and floating-point numbers. It supports standard arithmetic operations and provides a built-in Math object for advanced calculations.

Basic Number Operations

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (remainder)
  • ** Exponentiation (power)

📌 Deep Dive: Simple Arithmetic

JAVASCRIPT
let a = 10;
let b = 3;

console.log(a + b);  // 13
console.log(a - b);  // 7
console.log(a * b);  // 30
console.log(a / b);  // 3.3333333333333335
console.log(a % b);  // 1
console.log(a ** b); // 1000
Output
13
7
30
3.3333333333333335
1
1000

Important Number Properties

  • Infinity and -Infinity represent overflow values.
  • NaN means "Not a Number" (invalid number result).
  • Number.MAX_SAFE_INTEGER is the largest safe integer (253 - 1).
Illustration of numbers math
Illustration of numbers math

💡 Floating-Point Precision

Decimal calculations can sometimes be imprecise due to binary representation. For example, 0.1 + 0.2 !== 0.3.

The Math Object

The Math object provides useful constants and functions for advanced math:

Common Math Properties & Methods
Property / MethodDescription
Math.PIThe value of π (~3.14159)
Math.abs(x)Absolute value of x
Math.round(x)Rounds x to nearest integer
Math.floor(x)Rounds x down
Math.ceil(x)Rounds x up
Math.sqrt(x)Square root of x
Math.pow(x, y)x raised to the power y
Math.random()Random number between 0 (inclusive) and 1 (exclusive)

📌 Deep Dive: Using Math Methods

JAVASCRIPT
console.log(Math.PI);           // 3.141592653589793
console.log(Math.abs(-5));         // 5
console.log(Math.round(4.7));      // 5
console.log(Math.floor(4.7));      // 4
console.log(Math.ceil(4.3));       // 5
console.log(Math.sqrt(16));        // 4
console.log(Math.pow(2, 3));       // 8
console.log(Math.random());        // e.g. 0.721345...
Output
3.141592653589793
5
5
4
5
4
8
0.721345...

⚠️ Beware of Floating Point Arithmetic

Use methods like toFixed() or rounding to handle precision issues in decimal numbers.

Converting to Numbers

You can convert values to numbers using:

  • Number(value) - converts to a number or NaN if invalid
  • parseInt(string, radix) - parses an integer from a string
  • parseFloat(string) - parses a floating-point number from a string

📌 Deep Dive: Number Conversion

JAVASCRIPT
console.log(Number("123"));      // 123
console.log(Number("123abc"));    // NaN
console.log(parseInt("123abc"));  // 123
console.log(parseInt("0xF", 16)); // 15
console.log(parseFloat("3.14abc"));// 3.14
Output
123
NaN
123
15
3.14

💡 Type Coercion in Arithmetic

JavaScript automatically converts strings to numbers in math operations when possible, but concatenates if + is used with a string.

Summary

  • Numbers in JS are all floating-point (64-bit IEEE 754).
  • Use arithmetic operators for basic calculations.
  • The Math object offers many useful math functions.
  • Be cautious with floating-point precision and type coercion.