Menu Close

JavaScript Math Tutorial

In this article, you will learn about JavaScript Math. In the previous tutorial, we have seen all about JavaScript Break and continue statements with the help of examples.

In this guide, we will learn all about JavaScript Math properties and methods and their usages.

JavaScript Math

JavaScript Math provides a facility to perform a mathematical operation on numbers. Let’s understand all the javascript properties and methods with appropriate examples.

Number To Integer

In JavaScript Math, There are four common methods to convert Number to Integer.

  • Math.round(x):- Return x rounded to its nearest integer.
  • Math.ceil(x):- Return x rounded up to its nearest integer.
  • Math.floor(x):- Return x rounded down to its nearest integer.
  • Math.trunc(x):- Return the integer part of x.

Math.round()

Math.round(x) returns the value of x rounded to its nearest integer.

<script>
console.log(Math.round(12.8));
console.log(Math.round(12.4));
</script>

Math.ceil()

Math.ceil(x) method return the x rounded up to its nearest integer.

<script>
var x = Math.ceil(12.7);
// Value of x will be 13.
alert(x);
</script>

Math.floor(x)

Math.floor(x) return the x rounded to its nearest integer.

<script>
var x = Math.floor(12.7);
// Value of x will be 12.
console.log(x);
</script>

Math.trunc(x)

Math.trunc(x) return the only integer part of x.

<script>
var x = Math.trunc(12.7);
// Value of x will be 12.
console.log(x);
</script>

Math.pow()

Math.pow(x, y) return the power of x to the power of y.

<script>
// declare the variable x
var x = Math.pow(2, 8);
// Value of x will be 256.
alert(x);
</script>

Math.sqrt(x)

Math.sqrt(x) returns the square root of x.

<script>
// declare the variable x
var x = Math.sqrt(64);
// Value of x will be 8.
alert(x);
</script>

Math.abs(x)

Math.abs(x) return the absolute (positive) value of x.

<script>
console.log(-19);
</script>

Math.sin(x)

Math.sin(x) return the sin of angle x.

<script>
console.log(Math.sin(90));
</script>

Math.cos(x)

Math.sin(x) return the sin of angle x.

<script>
console.log(Math.cos(0));
</script>

Math.min() and Math.max()

Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments.

<script>
console.log(Math.max(12, 23, -34, 70));
console.log(Math.min(12, 23, -34, 70));
</script>

Math.random()

Math.random() return the random number from 0 (include) to 1 (exclude).

<script>
console.log(Math.random());
</script>

JavaScript Math Properties

In JavaScript, There are lots of properties.

console.log(Math.E)
console.log(Math.PI)
console.log(Math.SQRT2)
console.log(Math.SQRT1_2)
console.log(Math.LN2)
console.log(Math.LN10)
console.log(Math.LOG2E)
console.log(Math.LOG10E)

Conclusion

In this article, we have learned all about Math in JavaScript and its properties and methods.JavaScript Math is going to be the best option If you want to perform mathematical operations on numbers.

JavaScript Math provides various methods and properties that are very helpful to perform math operations.
If this JavaScript Math tutorial helpful for you, don’t forget to share.

JavaScript Comparison Operators
JavaScript While Loop

Related Posts