Mathematical Operations in JavaScript

Mathematical operations can be done using Math class.

To get the absolute value, you can use below line of code.
console.log(“Absolute value of a -> ” + Math.abs(a))

To get the ceiling of a number, you can use below line of code
console.log(“Ceiling value of a -> ” +Math.ceil(a))

To get the floor of a number, you can use below line of code
console.log(“Floor value of a -> ” +Math.floor(a))

To get the square root of a number, you can use below line of code.
console.log(“Square root value of a -> ” +Math.sqrt(625))

To find the maximum of 2 numbers, you can use below line of code.
console.log(“max between a and b -> ” +Math.max(a,b))

To find the minimum of 2 numbers, you can use below line of code.
console.log(“Min value between a and b -> ” +Math.min(a,b))

To get the power of a number, you can use below line of code.
console.log(“a raised to power b -> ” + Math.pow(a,b))

To get the random number between 0 and 1, you can use below method random()
console.log(“Random number between 0 and 1 -> ” + Math.random())

To round the number, you can use below line of code. Note that round method returns the integer.

console.log(“Round value of a -> ” + Math.round(a))

exp method returns the value of e^a
console.log(“Exponent of a -> ” + Math.exp(a))

To get the log of a to base e, you can use below line of code.
console.log(“Log of a ” + Math.log(a))

Math class also provides Trigonometric methods as mentioned below.
Math.sin(a), Math.cos(a), Math.tan(a), Math.acos(a)

You may also like...