JavaScript & jQuery Tutorials

JS Home Right Arrow JS Advanced Right Arrow Math

Math

The Math global object is a static object which means we cannot instantiate objects of type Math. All the properties and methods of this class are also static and so we access these using syntax such as Math.aPropertyName or Math.aMethodName.

In this lesson we explore some of the properties and methods of the Math object with examples of their usage.

Math Properties.

Let's look at the properties available for use with the Math object.



var mathProps = new Array(8);

/*
 * Store properties in an array.
 */ 
mathProps[0] = Math.E; // Euler's constant
mathProps[1] = Math.LN2; // Natural log of 2
mathProps[2] = Math.LN10; // Natural log of 10
mathProps[3] = Math.LOG2E; // Base 2 log of E
mathProps[4] = Math.LOG10E; // Base 10 log of E
mathProps[5] = Math.PI; // Circumference to diameter ratio of a circle
mathProps[6] = Math.SQRT1_2; // Square root of 1/2
mathProps[7] = Math.SQRT2; // Square root of 2

alert(mathProps);  

Press the button below to action the above code:



Some Math Methods.

Let's look at some examples of Math methods. For a complete list of Math methods and their usages go to the reference section of the site.



var mathMethods = new Array(5);

/*
 * Store results in an array.
 */ 
mathProps[0] = Math.max(10,50,30,12); // Return largest number
mathMethods[1] = Math.min(10,50,30,12); // Return smallest number
mathMethods[2] = Math.cos(25); // Return cosine of a number
mathMethods[3] = Math.sin(25); // Return sine of a number
mathMethods[4] = Math.sqrt(80); // Return positive square root of a number

alert(mathMethods);  

Press the button below to action the above code:



Lesson 4 Complete

In this lesson we looked at the Math static global object and some of its properties and methods.


Related Tutorials

JavaScript Basic Tutorials - Lesson 5 - Basic Maths Functions
JavaScript Intermediate Tutorials - Lesson 6 - More Maths Functions
JavaScript Advanced Tutorials - Lesson 3 - Number


What's Next?

In the next lesson we look at elegant object creation using prototyping.

Reference

JavaScript Reference - Math static object
JavaScript Reference - new special operator




  

go to home page Homepage go to top of page Top