Chapter 16. Math Function
Conceptual Overview of the Built-In Math Object
The Math
object contains static
properties and methods for mathematically dealing with numbers or
providing mathematical constants (e.g., Math.PI;
). This object is built into JavaScript,
as opposed to being based on a Math()
constructor that creates math instances.
Note
It might seem odd that Math
starts with a capitalized letter since you do not instantiate an
instance of a Math
object. Do not be
thrown off by this. Simply be aware that JavaScript sets this object up
for you.
Math Properties and Methods
The Math object has the following properties and methods:
Properties (e.g., Math.PI;
):
E
LN2
LN10
LOG2E
LOG10E
PI
SQRT1_2
SQRT2
Methods (e.g., Math.random();
):
abs()
acos()
asin()
atan()
atan2()
ceil()
cos()
exp()
floor()
log()
max()
min()
pow()
random()
round()
sin()
sort()
tan()
Math Is Not a Constructor Function
The Math
object is unlike the
other built-in objects that are instantiated. Math
is a one-off object created to house static
properties and methods, ready to be used when dealing with numbers. Just
remember, there is no way to create an instance of Math
, as there is no constructor.
Math Has Constants You Cannot Augment/Mutate
Many of the Math
properties are
constants
that cannot be mutated. Since this is a departure from the mutable nature
of JavaScript, these properties are in all-caps (e.g., Math.PI;
). Do not confuse these property constants for constructor functions due to the capitalization of their first letter. They are simply ...
Get JavaScript Enlightenment now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.