JavaScript Math Methods

The JavaScript Math is a built-in object that provides properties and methods for mathematical constants and functions to execute mathematical operations. It is not a function object, not a constructor. 

The Math functions consist of methods and properties. Following is the list of methods used with the Math object:

1. Math.round()

This method provides the value of the given number to a rounded integer. It can be written as:
Math.round(x), where x is a number.

2. Math.pow()

It provides the value of x to the power of y. It can be written as:
Math.pow(x, y), where x is a base number and y is an exponent to the given base.

3. Math.sqrt()

It gives the square root of a given integer. It can be written as:
Math.sqrt(x), where x is a number.

4. Math.abs()

It provides the absolute i.e. positive value of a number. It can be written as:
Math.abs(x); where x is a number.

5. Math.ceil()

It gives a smaller number, which is greater or equal to the given integer. It can be written as:
Math.ceil(x); where x is a number

6. Math.floor()

It gives a larger number, which is lesser or equal to the given integer. It can be written as:
Math.floor(x); where x is a number.

7. Math.sin()

It provides a sine of the given number. It can be written as:
Math.sin(x); where x is a number.

8. Math.cos()

It provides cosine of the given number. It can be written as:
Math.cos(x); where x is a number

9. Math.min() and Math.max()

The min() method is used to display the lowest value of the given arguments. It can be written as:
Math.min(val1, val2………valn); where val1, val2………valn are numbers.

The max() method is used to display the highest value of the given arguments. It can be written as:
Math.max(val1, val2………valn); where val1, val2………valn are numbers.

10. Math.random()

It provides a random number between 0 and 1. It can be written as:
Math.random();

11. Math.acos()

It provides an arccosine of an integer. It can be written as:
Math.acos(x); where x is a number.

12. Math.asin()

It provides arcsine of an integer. It can be written as:
Math.asin(x); where x is a number.

Examples

Let us see few examples for the above some methods of JavaScript Math Functions:

Math.abs()

Code:

<!DOCTYPE html>
<html>
<body>
<p id="abs_demo"></p>
<script>
document.getElementById("abs_demo").innerHTML = Math.abs(-5.6);
</script>
</body>
</html>

Output:

Math.abs() JavaScript

Math.ceil()

Code:

<!DOCTYPE html>
<html>
<body>
<p id="ceil_demo"></p>
<script>
document.getElementById("ceil_demo").innerHTML = Math.ceil(7.8);
</script>
</body>
</html>

Output:

Math.ceil() JavaScript

Math.floor()

Code:

<!DOCTYPE html>
<html>
<body>
<p id="floor_demo"></p>
<script>
document.getElementById("floor_demo").innerHTML = Math.floor(5.8);
</script>
</body>
</html>

Output:

Math.floor() JavaScript

Math.sin()

Code:

<html>
<body>
<script type = "text/javascript">
var value = Math.sin( 4.5 );
document.write("First Value : " + value );
var value = Math.sin( 90 );
document.write("<br />Second Value : " + value );
var value = Math.sin( Math.PI/2 );
document.write("<br />Third Value : " + value );
</script>
</body>
</html>

Output:

Math.sin() JavaScript

Math.cos()

Code:

<html>
<body>
<script type = "text/javascript">
var value = Math.cos(90);
document.write("First Value : " + value );
var value = Math.cos(-1);
document.write("<br />Second Value : " + value );
var value = Math.cos(2*Math.PI);
document.write("<br />Third Value : " + value );
</script>
</body>
</html>

Output:

Math.cos()

Math.min() and Math.max()

Code:

<!DOCTYPE html>
<html>
<body>
Minimum Value: <p id="min_demo"></p>
Maximum Value: <p id="max_demo"></p>
<script>
document.getElementById("min_demo").innerHTML =
Math.min(40, 87, 55, 25, 78, 14);
document.getElementById("max_demo").innerHTML =
Math.max(50, 90, 55, 25, 78, 14);
</script>
</body>
</html>

Output:

Math.min() and Math.max()

Math.random()

Code:

<html>
<body>
<script type = "text/javascript">
var value = Math.random( );
document.write("First Value : " + value );
var value = Math.random( );
document.write("<br />Second Value : " + value );
var value = Math.random( );
document.write("<br />Third Value : " + value );
</script>
</body>
</html>

Output:

Math.random()

Math.acos()

Code:

<html>
<body>
<script type = "text/javascript">
var value1 = Math.acos(-1);
document.write("First Value : " + value1 );
var value2 = Math.acos(null);
document.write("<br />Second Value : " + value2 );
var value3 = Math.acos(30);
document.write("<br />Third Value : " + value3 );
var value4 = Math.acos("string");
document.write("<br />Fourth Value : " + value4 );
</script>
</body>
</html>

Output:

Math.asin()

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…