JavaScript Math Object

In this chapter, we will learn about the JavaScript Math object. The Math object is a built-in object that provides properties and methods for mathematical constants and functions. We will cover:

  • What is the Math Object?
  • Math Properties
  • Math Methods
  • Working with Math Functions
  • Simple Programs using Math Object

What is the Math Object?

The Math object in JavaScript is a built-in object that provides mathematical constants and functions. It allows you to perform various mathematical operations such as rounding, exponentiation, logarithms, trigonometry, and more.

Math Properties

The Math object has several properties that represent mathematical constants.

Example

console.log(Math.PI); // Ratio of a circle's circumference to its diameter console.log(Math.E); // Euler's number console.log(Math.LN2); // Natural logarithm of 2 console.log(Math.LN10); // Natural logarithm of 10 console.log(Math.LOG2E); // Base 2 logarithm of E console.log(Math.LOG10E); // Base 10 logarithm of E console.log(Math.SQRT2); // Square root of 2 console.log(Math.SQRT1_2); // Square root of 1/2 

Output:

3.141592653589793 2.718281828459045 0.6931471805599453 2.302585092994046 1.4426950408889634 0.4342944819032518 1.4142135623730951 0.7071067811865476 

Math Methods

The Math object provides several methods to perform mathematical operations.

Math.abs()

Returns the absolute value of a number.

console.log(Math.abs(-10)); 

Output:

10 

Math.ceil()

Returns the smallest integer greater than or equal to a number.

console.log(Math.ceil(4.2)); 

Output:

5 

Math.floor()

Returns the largest integer less than or equal to a number.

console.log(Math.floor(4.8)); 

Output:

4 

Math.round()

Returns the value of a number rounded to the nearest integer.

console.log(Math.round(4.5)); 

Output:

5 

Math.max()

Returns the largest of zero or more numbers.

console.log(Math.max(1, 2, 3, 4, 5)); 

Output:

5 

Math.min()

Returns the smallest of zero or more numbers.

console.log(Math.min(1, 2, 3, 4, 5)); 

Output:

1 

Math.pow()

Returns the base to the exponent power.

console.log(Math.pow(2, 3)); 

Output:

8 

Math.sqrt()

Returns the positive square root of a number.

console.log(Math.sqrt(16)); 

Output:

4 

Math.random()

Returns a pseudo-random number between 0 and 1.

console.log(Math.random()); 

Output:

0.123456789 (example output) 

Math.log()

Returns the natural logarithm (base E) of a number.

console.log(Math.log(Math.E)); 

Output:

1 

Math.sin(), Math.cos(), Math.tan()

Returns the sine, cosine, and tangent of a number (angle in radians).

console.log(Math.sin(Math.PI / 2)); console.log(Math.cos(Math.PI)); console.log(Math.tan(Math.PI / 4)); 

Output:

1 -1 1 

Working with Math Functions

You can use the Math object to perform various mathematical operations and solve mathematical problems.

Example

let radius = 5; let area = Math.PI * Math.pow(radius, 2); console.log("Area of the circle:", area); 

Output:

Area of the circle: 78.53981633974483 

Simple Programs using Math Object

Program 1: Calculating the Hypotenuse of a Right-Angled Triangle

function calculateHypotenuse(a, b) { return Math.sqrt(Math.pow(a, b) + Math.pow(b, 2)); } let a = 3; let b = 4; let hypotenuse = calculateHypotenuse(a, b); console.log("Hypotenuse:", hypotenuse); 

Output:

Hypotenuse: 5 

Program 2: Generating a Random Number within a Range

function getRandomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } let randomNumber = getRandomNumber(1, 10); console.log("Random Number between 1 and 10:", randomNumber); 

Output:

Random Number between 1 and 10: 7 (example output) 

Conclusion

In this chapter, you learned about the JavaScript Math object, including its properties, methods, and how to use it to perform various mathematical operations. We also provided various use cases with simple programs to demonstrate the usage of the Math object. Understanding how to effectively use the Math object is essential for performing mathematical calculations in JavaScript.

Leave a Comment

Scroll to Top