Math Function

The Math object contains static properties and methods for mathematically dealing with numbers or providing mathematical constants (Math.PI;). This object is built into JavaScript, as opposed to being based on a Math() constructor that creates math instances.

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 (Math.PI;):

Methods (Math.random();):


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 or 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 (Math.PI;). Do not confuse these property constants for constructor functions due to the capitalization of their first letter. They are simply object properties that cannot be changed.

User-defined constants are not possible in JavaScript 1.5, ECMA-262, Edition 3.


Conclusion

You should now have a few new tools that you can use for any math related tasks with JavaScript.

Tags:

Comments

Related Articles