the user. One of these is the
Math object, which contains more than the
standard mathematical operations (
+, -, *, /).
In the previous sections, we learned how to construct, modify, and use objects
by the user.
Unlike other objects, the
Mathobject is immutable.
Math Properties Are Constants¶
Math object has 8 defined properties. These represent mathematical
constants, like the value for pi (π) or the square root of 2.
Instead of defining a variable to hold as many digits of pi as we can remember,
create a new object. By using dot notation and calling
Math.PI, we can
access the value of pi.
1 2 3
console.log(Math.PI); console.log(Math.PI*4); console.log(Math.PI + Math.PI);
3.141592653589793 12.566370614359172 6.283185307179586
As stated above, the properties within
Math cannot be changed by the
1 2 3 4 5
console.log(Math.PI); Math.PI = 1234; console.log(Math.PI);
To use one of the other constants stored in
Math, we replace
the property name (e.g.
SQRT2 stores the value for the square root of 2).
More powerful uses of the
Math object involve using methods, which we
will examine next.