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.
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.
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.
console.log(Math.ceil(4.2)); // 5
Flooring
Math.floor(number)
rounds the number down to the nearest integer.
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.
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.
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.
console.log(Math.sqrt(16)); // 4 (Square root of 16)
Which of the following is the most appropriate to fill in the blank?
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result