Skip to main content
Practice

Math Object

JavaScript offers various functionalities to help solve mathematical problems.

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


Basic Constants

  • Math.PI: Returns the value of π (Pi), which is the ratio of a circle's circumference to its diameter.
Output the value of Pi
console.log(Math.PI); // 3.141592653589793

Using Math.PI, you can easily calculate the area or circumference of a circle.


Rounding, Ceiling, Flooring

  • 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

  • Math.ceil(number): Rounds the number up to the nearest integer.
Math.ceil Ceiling
console.log(Math.ceil(4.2)); // 5

  • Math.floor(number): Rounds the number down to the nearest integer.
Math.floor Flooring
console.log(Math.floor(4.7)); // 4

Maximum, 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

Power, Square Root

  • Math.pow(base, exponent): Returns the base to the exponent power.
Output power value
console.log(Math.pow(2, 3)); // 8 (2 raised to the power of 3)

  • 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.