Skip to Content
JavaScript Enlightenment
book

JavaScript Enlightenment

by Cody Lindley
December 2012
Beginner
164 pages
3h 15m
English
O'Reilly Media, Inc.
Content preview from JavaScript Enlightenment

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;):

  • E

  • LN2

  • LN10

  • LOG2E

  • LOG10E

  • PI

  • SQRT1_2

  • SQRT2

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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript: Best Practice

JavaScript: Best Practice

James Kolce, Moritz Kroger, Ivan Curic, Samier Saeed, Jeff Mott, M. David Green, Craig Buckler
CSS Secrets

CSS Secrets

Lea Verou
DOM Enlightenment

DOM Enlightenment

Cody Lindley
Deno Web Development

Deno Web Development

Alexandre Portela dos Santos

Publisher Resources

ISBN: 9781449344412Errata Page