Lecture

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)
Mission
0 / 1

Which of the following is the most appropriate to fill in the blank?

The method to round a value down to the nearest smaller integer using JavaScript's Math object is .
Math.round
Math.ceil
Math.floor
Math.min

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run

Execution Result