JavaScript Tutorials - Herong's Tutorial Examples - 2.33, by Herong Yang
The "Math" Object Type - The Math Container
This section provides a quick description and a tutorial example script on the 'Math' built-in object type. There is only one default 'Math' object called 'Math' that holds all math properties and functions.
The "Math" object type is a special built-in object type, that is used by the host environment to create a single "Math" object as a container for many mathematics related properties and functions. It has the following main features:
Here is a tutorial example script showing some of those features:
// Math_Object_Type.js // Copyright (c) 2013 HerongYang.com. All Rights Reserved. // Creating your own "Math" objects is not allowed // var myObject = new Math(); // Checking this object println("\nAbout this \"Math\":"); println(" Type = "+(typeof Math)); println(" Instance Of Object: "+(Math instanceof Object)); println(" Instance Of Function: "+(Math instanceof Function)); println(" Instance Of Math: "+(Math instanceof Math)); println(" toString(): "+Math.toString()); println("\n\"Math\" value properties:"); println(" Math.E = "+Math.E); println(" Math.PI = "+Math.PI); println(" Math.SQRT2 = "+Math.SQRT2); println("\nCalling \"Math\" function properties - methods:"); println(" Math.sqrt(2) = "+Math.sqrt(2)); println(" Math.sin(Math.PI) = "+Math.sin(Math.PI)); println(" Math.abs(-17.78) = "+Math.abs(-17.78)); println(" Math.ceil(-17.78) = "+Math.ceil(-17.78)); println(" Math.floor(-17.78) = "+Math.floor(-17.78)); println(" Math.round(-17.78) = "+Math.round(-17.78)); println(" Math.random() = "+Math.random());
If you run this script with "jrunscript", you will get:
About this "Math": Type = object Instance Of Object: true Instance Of Function: false Instance Of Math: false toString(): [object Math] "Math" value properties: Math.E = 2.718281828459045 Math.PI = 3.141592653589793 Math.SQRT2 = 1.4142135623730951 Calling "Math" function properties - methods: Math.sqrt(2) = 1.4142135623730951 Math.sin(Math.PI) = 1.2246467991473532e-16 Math.abs(-17.78) = 17.78 Math.ceil(-17.78) = -17 Math.floor(-17.78) = -18 Math.round(-17.78) = -18 Math.random() = 0.4715524330340368
Now we know how to use the "Math" object.
Read other "Math" related chapters to see more tutorial examples.
Table of Contents
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
Defining Your Own Object Types
Inheritance of Properties and Methods through the Prototype Object Chain
'jrunscript' - JavaScript Shell Command from JDK
Using Functions as "Function" Objects
►Introduction to Built-in Object Types
Overview of Built-in Object Types
The "Object" Object Type - The Root Object Type
The "Global" Object Type - The Invisible Global Container
Global Properties and Functions Defined in ECMAScript
Global Properties and Functions Provided by "jrunscript"
The "Function" Object Type - Functions Are Objects
The "Array" Object Type - Arrays Are Objects
The "JSON" Object Type - parse() and stringify()
The "String" Object Type - Not Equal to String Primitive Type
The "Boolean" Object Type - Wrapping Boolean Values into Objects
The "Number" Object Type - Not Equal to Number Primitive Type
The "Date" Object Type - Managing Dates and Times
The "RegExp" Object Type - Regular Expression Patterns
The "Error" Object Type - Runtime Exceptions
►The "Math" Object Type - The Math Container
W3C's Document Object Model (DOM) Specifications