Wednesday , 17 January 2018
Home / Programming / JavaScript / Math Object – JavaScript Tutorial
Math Object
Math Object

Math Object – JavaScript Tutorial

This tutorial is part of the JavaScript tutorial series for beginners. To view the whole list of tutorials available and the order in which you should follow them, then click here.

The math object is another object you’ll find yourself using often. It allows you to perform advance math operations. It can really save you time and you don’t have to write as much code.

Math Object Properties

Let’s get started. Clear your script and add this bit of code.

You do not have to create an instance of the math object. You can use the math object directly with the Math keyword. The math object has a couple of properties, but we’ll only focus on 1. The PI property simply returns the value of PI. The real power of the math object is the methods it contains.

Math Object Methods

Add this bit of code into your script.

There are a couple of methods to go over. You should get something close to this.


Here is what each method does

  1. ceil() – This method will round any number to the nearest integer. An integer is a number without a decimal. It is important to know that this method will round up no matter what. So, a number like 4.1 will always be rounded up to 5.
  2. floor() – This method is similar to the ceil() method except it rounds down no matter what. So, a number like 4.9 will always be rounded to 4.
  3. round() – This method will round a number to the nearest integer. Unlike the floor() and ceil() methods, this method will round the number up or down depending on the decimal values. A number like 4.5 will round to 5.
  4. pow(x, y) – This method will return the value of x to the power of y. x being the value in the first parameter and y being the value in the second parameter.
  5. random() – This method will return a completely random number from 0 to 1.
  6. sqrt( x ) – This method will return the square root of x. x being the value passed in the first parameter.

We’ll be using these methods in future tutorials when we start building real world applications


The math object has plenty of properties and methods to use. If you’re unsure on how to do a certain math operation, consider looking into the math object to do what you’re looking for. Here’s a link for more information about the math object. It also includes a list of all properties and methods related to the math object.

About Jasko Koyn

Check Also

Try-Catch Statement

Try-catch Statement – Advanced JavaScript Tutorial

The try-catch statement is a unique way to handle errors. When errors occur, usually it …

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to learn how to create web applications with Node.js and SQL? Check out my course!
Get 93% off my course. You'll also learn how to build real world applications using Node and SQL. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!