Numbers and Math in JavaScript

Working with Numbers and Math in JavaScript

Numbers are one of the core data types in JavaScript and are heavily used in everything from UI layout calculations to backend logic. This module takes a deep dive into numeric operations, math functions, and some of the quirks you need to be aware of when dealing with numbers in JavaScript.


Table of Contents

  1. JavaScript Number Type Overview
  2. Arithmetic Operators
  3. Dealing with Floating-Point Precision
  4. Math Object and Common Methods
  5. Rounding Numbers
  6. Random Numbers
  7. Number Conversion
  8. Number Checking and Validation
  9. BigInt: Handling Very Large Integers
  10. Conclusion

1. JavaScript Number Type Overview

JavaScript has a single number type for both integers and floating-point numbers:

let age = 25;         // integer
let price = 19.99; // floating point

Under the hood, JavaScript uses 64-bit floating-point numbers (IEEE 754), which means it handles most typical operations well — but can sometimes be inaccurate with floating points (e.g., 0.1 + 0.2 !== 0.3).


2. Arithmetic Operators

JavaScript supports the standard set of arithmetic operators:

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.333...
console.log(a % b); // 1 (modulo)

Also includes increment/decrement:

a++; // 11
b--; // 2

3. Dealing with Floating-Point Precision

console.log(0.1 + 0.2); // 0.30000000000000004

To fix this, round the number:

let result = (0.1 + 0.2).toFixed(2); // "0.30"
console.log(Number(result)); // 0.3

Or use libraries like decimal.js for more precision.


4. Math Object and Common Methods

The Math object provides static methods for performing mathematical operations:

Math.abs(-5);         // 5
Math.pow(2, 3); // 8
Math.sqrt(16); // 4
Math.max(10, 5, 7); // 10
Math.min(10, 5, 7); // 5
Math.floor(4.9); // 4
Math.ceil(4.1); // 5
Math.round(4.5); // 5
Math.trunc(4.9); // 4

5. Rounding Numbers

Different types of rounding:

Math.floor(4.7);  // 4
Math.ceil(4.2); // 5
Math.round(4.5); // 5
Math.trunc(4.9); // 4

For decimal precision:

let num = 1.2345;
console.log(num.toFixed(2)); // "1.23"
console.log(num.toPrecision(3)); // "1.23"

6. Random Numbers

Generate random numbers using Math.random():

let rand = Math.random(); // between 0 and 1

For a range:

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(getRandomInt(1, 100)); // e.g., 42

7. Number Conversion

Convert strings to numbers:

Number("123");     // 123
parseInt("10px"); // 10
parseFloat("10.5");// 10.5
+"5.5"; // 5.5

8. Number Checking and Validation

Use these to check and validate numeric values:

isNaN("abc");        // true
isFinite(10); // true
Number.isNaN(NaN); // true
Number.isInteger(5); // true

9. BigInt: Handling Very Large Integers

JavaScript supports BigInt for integers beyond Number.MAX_SAFE_INTEGER (2^53 – 1):

let big = 1234567890123456789012345678901234567890n;
console.log(big * 2n);

Note:

  • BigInt literals end with n
  • BigInt cannot be mixed with regular numbers in arithmetic

10. Conclusion

Working with numbers in JavaScript goes beyond basic arithmetic. Understanding rounding, floating-point quirks, the Math object, and BigInt helps you avoid common pitfalls and write more accurate code.

In the next module, we’ll focus on Dates and Times — another frequently used and often misunderstood aspect of JavaScript programming.