About Lesson
The Math
object is a built-in object in JavaScript that has properties and methods for mathematical operations. It’s not a function object, it’s not a constructor for Math
objects, they are static (belong to the Math
object itself).
Properties of the Math Object
The Math
object has several properties that represent mathematical constants. Here are a few:
Math.E
: Euler’s number, the base of natural logarithms (approximately 2.718).Math.PI
: Ratio of the circumference of a circle to its diameter (approximately 3.14159).Math.SQRT2
: Square root of 2 (approximately 1.414).
Methods of the Math Object
The Math
object also has several methods that are used for mathematical operations. Here are a few:
Math.abs(x)
: Returns the absolute value ofx
.Math.ceil(x)
: Returns the smallest integer greater than or equal tox
.Math.floor(x)
: Returns the largest integer less than or equal tox
.Math.max(x, y, z, ..., n)
: Returns the number with the highest value.Math.min(x, y, z, ..., n)
: Returns the number with the lowest value.Math.pow(x, y)
: Returns the value ofx
to the power ofy
.Math.random()
: Returns a random number between 0 (inclusive) and 1 (exclusive).Math.round(x)
: Roundsx
to the nearest integer.Math.sqrt(x)
: Returns the square root ofx
.
The Math
object is a powerful tool in JavaScript, that allows you to perform complex mathematical operations in your code. It’s a fundamental concept that’s widely used in programming, so understanding how it works will greatly enhance your JavaScript coding skills.