Numbers and Math in javascript

In JavaScript, numbers are used everywhere—whether you're counting items, doing calculations, or building logic based on numeric values. JavaScript has built-in tools to handle numbers in both simple and advanced ways. From basic addition to generating random values, this lesson will guide you through numbers and the Math object in JavaScript using simple, beginner-friendly language.

Number Constructor

You can create a number in JavaScript in two ways: by writing it directly or by using the Number() constructor.

javascript
1
2
          let age = 25; // Direct
let price = Number("49.99"); // Using the constructor
        

The Number() constructor is useful when converting text into numbers:

javascript
1
2
          let num = Number("123"); // Becomes 123
let invalid = Number("abc"); // Becomes NaN (Not a Number)
        

Integers and Decimals

JavaScript does not have separate types for integers and decimals—both are treated as numbers.

javascript
1
2
          let wholeNumber = 10;       // Integer
let decimalNumber = 10.5;   // Decimal
        

These numbers can be positive, negative, or zero. You can also use them in math operations:

javascript
1
2
          console.log(5 + 2); // Output: 7
console.log(10.5 - 3); // Output: 7.5
        

toFixed()

The toFixed() method controls how many digits appear after the decimal point. It returns a string.

javascript
1
2
          let price = 9.456;
console.log(price.toFixed(2)); // Output: "9.46"
        

This is often used for showing prices, measurements, or anything that needs rounding.

toPrecision()

toPrecision() controls the total number of digits in the number (not just after the decimal). It also returns a string.

javascript
1
2
          let number = 123.456;
console.log(number.toPrecision(4)); // Output: "123.5"
        

If the number is too small or too large, JavaScript may switch to scientific notation.

toExponential()

This method converts a number into exponential (scientific) notation.

javascript
1
2
          let num = 123000;
console.log(num.toExponential()); // Output: "1.23e+5"
        

It’s useful when working with very big or very small numbers.

toLocaleString()

toLocaleString() formats the number based on the local settings (like adding commas for thousands or using currency symbols).

javascript
1
2
          let salary = 1500000;
console.log(salary.toLocaleString()); // Output: "1,500,000" (in US English)
        

You can also format it as currency:

javascript
1
2
          console.log(salary.toLocaleString('en-US', { style: 'currency', currency: 'USD' }));
// Output: "$1,500,000.00"
        

MAX_VALUE and MIN_VALUE

JavaScript has built-in constants for the largest and smallest numbers it can handle.

javascript
1
2
3
          console.log(Number.MAX_VALUE); // The biggest number possible
console.log(Number.MIN_VALUE); // The smallest positive number (very close to zero)
// These are helpful when checking for safe ranges in calculations
        

NaN and isNaN()

When a value is "Not a Number", JavaScript returns NaN. This often happens when math is done on something that's not numeric.

javascript
1
2
          let result = Number("hello"); // NaN
console.log(isNaN(result)); // true
        

Use isNaN() to check if something is a valid number.

Infinity and -Infinity

JavaScript returns Infinity when a number gets too large or when dividing by zero.

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

These are still considered numbers in JavaScript.

Math Object in JavaScript

When working with numbers in JavaScript, the Math object gives you useful tools to do common math tasks. From basic operations like rounding numbers to generating random values, the Math object helps you save time and avoid writing complex code.

Basic Math Operations

JavaScript supports all the basic math operations using simple symbols:

javascript
1
2
3
4
5
6
7
8
          let a = 10;
let b = 3;

console.log(a + b); // Addition, Output: 13
console.log(a - b); // Subtraction, Output:  7
console.log(a * b); // Multiplication, Output: 30
console.log(a / b); // Division, Output: 3.333...
console.log(a % b); // Modulus – Remainder after division, Output: 1
        

You can also use parentheses to control the order:

javascript
1
          console.log((5 + 3) * 2); // Output: 16
        

Math.random()

This function gives you a random number between 0 (inclusive) and 1 (exclusive).

javascript
1
          console.log(Math.random()); // Example: 0.587124
        

To get a random whole number, multiply the result and round it:

javascript
1
2
          let randomNumber = Math.floor(Math.random() * 10); // 0 to 9
console.log(randomNumber);
        

You can adjust the range by changing the multiplier.

Math.floor()

This function rounds a decimal number down to the nearest whole number.

javascript
1
2
          console.log(Math.floor(4.9)); // Output: 4
console.log(Math.floor(7.1)); // Output: 7
        

It's commonly used with Math.random() to get whole numbers.

Math.ceil()

This function rounds a number up to the next whole number.

javascript
1
2
          console.log(Math.ceil(4.1)); // Output: 5
console.log(Math.ceil(6.8)); // Output: 7
        

Math.round()

Math.round() rounds a number to the nearest whole number.

javascript
1
2
          console.log(Math.round(4.4)); // Output: 4
console.log(Math.round(4.5)); // Output: 5
        

Math.max() and Math.min()

These functions return the highest or lowest number from a list of values.

javascript
1
2
          console.log(Math.max(3, 5, 1, 9)); // Output: 9
console.log(Math.min(3, 5, 1, 9)); // Output: 1
        

Math.sqrt()

Math.sqrt() returns the square root of a number.

javascript
1
          console.log(Math.sqrt(16)); // Output: 4
        

Math.pow()

This function is used to raise a number to a power.

javascript
1
          console.log(Math.pow(2, 3)); // (2³), Output: 8
        

You can also write it using the operator:

javascript
1
          console.log(2 ** 3); // Output: 8