Skip to main content
Crowdfunding
Python + AI for Geeks
Practice

Math Object

JavaScript provides several features to assist with solving mathematical problems.

The Math object provides various constants and methods to help with mathematical operations.


Basic Constants for Mathematical Operations

Math.PI returns the value of π (pi), which is the ratio of the circumference of a circle to its diameter.

Output the value of pi
console.log(Math.PI); // 3.141592653589793

Using Math.PI is useful when calculating the area or circumference of a circle.

Additionally, Math.E returns the value of the natural constant e.


Rounding

Math.round(number) rounds the number to the nearest integer.

Math.round Rounding
console.log(Math.round(4.7)); // 5
console.log(Math.round(4.4)); // 4

Ceiling

Math.ceil(number) rounds the number up to the nearest integer.

Math.ceil Ceiling
console.log(Math.ceil(4.2)); // 5

Flooring

Math.floor(number) rounds the number down to the nearest integer.

Math.floor Flooring
console.log(Math.floor(4.7)); // 4

Maximum and Minimum Values

  • Math.max(number1, number2, ...) : Returns the largest of the given numbers.

  • Math.min(number1, number2, ...) : Returns the smallest of the given numbers.

Output Maximum and Minimum Values
console.log(Math.max(3, 7, 2, 8, 5)); // 8
console.log(Math.min(3, 7, 2, 8, 5)); // 2

Exponents

Math.pow(base, exponent) calculates the power of the base raised to the exponent.

Output Exponent Value
console.log(Math.pow(2, 3)); // 8 (2 raised to the power of 3)

Square Roots

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

Output Square Root Value
console.log(Math.sqrt(16)); // 4 (Square root of 16)

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.