Lesson 25 of 36 · 2 min

The Math Object

Explore the JavaScript Math object with examples. Learn how to perform mathematical operations and utilize constants in your JavaScript code…

On this page
  1. Mathematical Constants
  2. Basic Mathematical Operations
  3. Exponents and Logarithms
  4. Trigonometric Functions

In JavaScript, the Math object is a built-in global object that provides a set of mathematical operations and constants. It allows developers to perform various mathematical calculations in their code. Let’s explore the Math object and some of its commonly used methods with examples.

Mathematical Constants

  1. Math.PI: This constant represents the mathematical constant Pi (approximately 3.141592653589793).
const pi = Math.PI;
console.log(pi); // 3.141592653589793
  1. Math.E: This constant represents the mathematical constant Euler’s number (approximately 2.718281828459045).
const e = Math.E;
console.log(e); // 2.718281828459045

Basic Mathematical Operations

  1. Math.abs(x): Returns the absolute (positive) value of a number.
const absoluteValue = Math.abs(-5);
console.log(absoluteValue); // 5
  1. Math.round(x): Rounds a number to the nearest integer.
const roundedValue = Math.round(3.7);
console.log(roundedValue); // 4
  1. Math.floor(x): Rounds a number down to the nearest integer.
const floorValue = Math.floor(4.9);
console.log(floorValue); // 4
  1. Math.ceil(x): Rounds a number up to the nearest integer.
const ceilValue = Math.ceil(2.1);
console.log(ceilValue); // 3
  1. Math.min(x1, x2, ..., xn): Returns the smallest of the provided numbers.
const smallestNumber = Math.min(5, 2, 8, 1);
console.log(smallestNumber); // 1
  1. Math.max(x1, x2, ..., xn): Returns the largest of the provided numbers.
const largestNumber = Math.max(5, 2, 8, 1);
console.log(largestNumber); // 8

Exponents and Logarithms

  1. Math.pow(x, y): Raises x to the power of y.
const result = Math.pow(2, 3);
console.log(result); // 8
  1. Math.sqrt(x): Returns the square root of a number.
const squareRoot = Math.sqrt(16);
console.log(squareRoot); // 4
  1. Math.log(x): Returns the natural logarithm (base e) of a number.
const naturalLog = Math.log(Math.E);
console.log(naturalLog); // 1

Trigonometric Functions

  1. Math.sin(x): Returns the sine of an angle in radians.
const sineValue = Math.sin(Math.PI / 2); // Sine of 90 degrees (π/2 radians)
console.log(sineValue); // 1
  1. Math.cos(x): Returns the cosine of an angle in radians.
const cosineValue = Math.cos(Math.PI); // Cosine of 180 degrees (π radians)
console.log(cosineValue); // -1
  1. Math.tan(x): Returns the tangent of an angle in radians.
const tangentValue = Math.tan(Math.PI / 4); // Tangent of 45 degrees (π/4 radians)
console.log(tangentValue); // 1

These are just some of the many mathematical operations and constants provided by the Math object in JavaScript. It’s a valuable tool for performing various calculations in web development, from basic arithmetic to more advanced mathematical operations. Developers often use the Math object to solve problems involving geometry, physics, and other mathematical domains in their JavaScript applications.

More from JavaScript Tutorial