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;):
ELN2LN10LOG2ELOG10EPISQRT1_2SQRT2
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access