Applications of JavaScript Math Object

How to use JavaScript Math Object to perform mathematical tasks easily

Dilka Sithari
LinkIT

--

Photo by Irvan Smith on Unsplash

After going through this article you will be able to get to know about many effective properties and methods to perform many mathematical tasks by using JavaScript math objects.

There are many methods to perform mathematical operations by using JavaScript math objects. In this article, I discuss a few math objects. By using the below reference links you can refer to other methods.

Math object is used for,

1. Returns the absolute value of a number.

Math.abs()

2. Rounded upward a number to the nearest integer.

Math.ceil()

3. Rounded downward a number to the nearest integer.

Math.floor()

4. Returns the base 10 logarithms of a number.

Math.log10()

Returns the base 2 logarithms of a number.

Math.log2()

5. Rounds off a number to the nearest integer.

Math.round()

6. Returns the value of a to the power b.

Math.power(a,b)

7. Calculates the square root of a number.

Math.sqrt()

8. Returns the highest number among a set of numbers.

Math.max()

Returns the lowest number among a set of numbers.

Math.min()

9. Returns a random number between 0 and 1.

Math.random()

10. Returns the value of the mathematical constant ₶= 3.14159

Math.PI

Let’s see how these math objects work with javascript programs.

1. Math.abs()

document.writeln(Math.abs(-88.12)); // output 88.12

The output of this program is 88.12 which is an absolute value of -88.12.

When calculating the Price Elasticity of Demand of a good the answer does not matter sign. So we need the absolute value.

var PED = (228–300)/40;document.writeln(Math.abs(PED)); // output 1.8

2. Math.ceil()

document.writeln(Math.ceil(2.2)); // output 3document.writeln(Math.ceil(100.25)); // output 101document.writeln(Math.ceil(0.1289)); // output 1document.writeln(Math.ceil(5003.012)); // output 5004document.writeln(Math.ceil(-8.7)); // output -8

These examples show that by using Math.ceil() decimal numbers return the next highest integer of that number.

3. Math.floor()

document.writeln(Math.floor(-6.4)); // output -7document.writeln(Math.floor(5.89)); // output 5document.writeln(Math.floor(45.987)); // output 45document.writeln(Math.floor(100.786)); // output 100document.writeln(Math.floor(78.98)); // output 78

These examples show that by using Math.floor() decimal numbers returns the next lowest integer of that number.

4. Math.log10()

When places where we want to insert a mathematical equation with logarithmic bases into our JavaScript code we can use Math.log10() for logarithmic functions with base 10. In the same way, we can use Math.log2() for the logarithmic calculations with base 2.

Think about we want to write a JavaScript code to calculate the attenuation introduced by a cable. There we need a logarithmic base 10 value.

document.writeln(10*Math.log10(63)); // output 17.993405494535818

In a similar manner, we can calculate the value of the numbers with the logarithmic base 2.

document.writeln(Math.log2(89)); // output 6.475733430966398

5. Math.round()

In Java script Math.round() is used to return the nearest integer value of a decimal number.

document.writeln(Math.round(7.789523)); // 8document.writeln(Math.round(0.15689)); // 0document.writeln(Math.round(5.623589)); // 6document.writeln(Math.round(2.56823)); // 3

6. Math.pow(a,b)

document.writeln(Math.pow(8.3,2)); // output 68.89000000000001

This program returns the value of 8.3 to the power 2. We can see that the decimal points are too lengthy. If we did not need decimal places we can remove those decimal places and round off this number to the nearest integer.

document.writeln(Math.round( Math.pow(8.3,2))); // output 69

Here I use two math objects Math.round() to round off the answer and Math.pow() to calculate the value of 8.3 to the power 2.

7. Math.sqrt()

The square root of a number can be calculated by using Math.sqrt() in JavaScript.

Example:

Calculate the hypotenuse of a right-angled triangle using the Pythagoras theorem. Values of the two sides which bear the right angle are 7.8 and 5.3.

Let’s see how to write a JavaScript program to calculate the answer.

document.writeln( Math.sqrt( Math.pow(7.8,2)+ Math.pow(5.3,2)));// 9.430270409696638

After applying Math.round() object answer gives by rounding off to the nearest integer as below method.

document.writeln( Math.round(Math.sqrt( Math.pow(7.8,2)+ Math.pow(5.3,2)))); // 9

8. Math.max()

If we want to return the highest number among a set of numbers we use Math.max() to write JavaScript codes.

The below code returns the highest number which is 1001.

document.writeln(Math.max(78,89,-56,1001,865,34,-9));// 1001

In the same manner Math.min() is used to return the smallest number among a set of numbers.

document.writeln(Math.min(78,89,-56,1001,865,34,-9));// -56

9. Math.random()

This code returns a random number between 0 and 1.

document.writeln(Math.random());

By multiplying the above code by 10 it returns a random number between 0 and 10.

document.writeln(Math.random()* 10);

By adding Math.round() to the above code it returns an integer value between 0 and 10.

document.writeln(Math.round(Math.random()* 10));

10. Math.PI

document.writeln(2*Math.PI*3.78); // 23.75044046113883

This is an example to calculate the circumference of a circle that has a radius of 3.78. Here the value of the constant ₶ is replaced by its value 3.141592653589793.

I think you gain some knowledge after reading my article.

Thank you for reading!

References:

--

--

Dilka Sithari
LinkIT
Writer for

Final Year Undergraduate at Faculty of Information Technology, University of Moratuwa, Sri Lanka.