The following programs demonstrate some Examples of Math Functions in JavaScript.

In order to find the description of Math functions in JavaScript, click here. Some of the examples of basic math functions in JavaScript are given below.

``````let num = -5;
console.log(Math.abs(num)); // 5
console.log(Math.ceil(num)); // -5
console.log(Math.floor(num)); // -5
console.log(Math.round(num)); // -5
console.log(Math.max(1, 2, 3, 4, 5)); // 5
console.log(Math.min(1, 2, 3, 4, 5)); // 1
console.log(Math.pow(2, 3)); // 8
console.log(Math.sqrt(16)); // 4
console.log(Math.exp(2)); // 7.389056098930649
console.log(Math.log(Math.E)); // 1
console.log(Math.PI); // 3.141592653589793``````

## More Examples of Math Functions in JavaScript

Also, keep in mind that the argument for `Math.log10(x)` must be positive and greater than 0, otherwise it will return `NaN` (Not a Number).

``````console.log(Math.log10(100)); // 2
console.log(Math.log10(1000)); // 3
console.log(Math.log10(10000)); // 4``````

Furthermore, the `Math.sign(x)` function in JavaScript returns the sign of a number, indicating whether the number is positive, negative, or zero. The function returns one of the following values:

• `1` if the number is positive
• `-1` when the number is negative
• `0` in case the number is zero
• `NaN` if the number is `NaN`

The following code shows an example.

``````console.log(Math.sign(5)); // 1
console.log(Math.sign(-5)); // -1
console.log(Math.sign(0)); // 0
console.log(Math.sign(NaN)); // NaN``````

Likewise, the `Math.sin(x)` function in JavaScript returns the sine of `x`, where `x` is an angle in radians. The following code shows an example.

``````console.log(Math.sin(0)); // 0
console.log(Math.sin(Math.PI / 2)); // 1
console.log(Math.sin(Math.PI)); // 0
console.log(Math.sin(3 * Math.PI / 2)); // -1``````

Also, note that the trigonometric functions in JavaScript, including `Math.sin(x)`, use radians as the unit for measuring angles. If you have an angle measured in degrees, you can convert it to radians using the following formula:

``````let radians = degrees * (Math.PI / 180);
``````

Similarly, the `Math.cos(x)` function in JavaScript returns the cosine of `x`, where `x` is an angle in radians. The following code shows an example.

``````console.log(Math.cos(0)); // 1
console.log(Math.cos(Math.PI / 2)); // 0
console.log(Math.cos(Math.PI)); // -1
console.log(Math.cos(3 * Math.PI / 2)); // 0
``````

Similarly, we can use the Math.tan() function. The following code shows an example.

``````console.log(Math.tan(0)); // 0
console.log(Math.tan(Math.PI / 4)); // 1
console.log(Math.tan(Math.PI / 2)); // Infinity
console.log(Math.tan(3 * Math.PI / 4)); // -1
``````

Also, keep in mind that the tangent of an angle in radians can be undefined (`NaN`) for certain angles, such as `Math.PI / 2`, which corresponds to 90 degrees. In this case, `Math.tan(Math.PI / 2)` returns `Infinity`.

Further, the `Math.tanh(x)` function in JavaScript returns the hyperbolic tangent of `x`. The hyperbolic tangent function is defined as:

``````tanh(x) = (e^x - e^-x) / (e^x + e^-x)
``````

The following code shows an example.

``````console.log(Math.tanh(0)); // 0
console.log(Math.tanh(1)); // 0.7615941559557649
console.log(Math.tanh(-1)); // -0.7615941559557649
console.log(Math.tanh(Infinity)); // 1
console.log(Math.tanh(-Infinity)); // -1
``````

Note that the hyperbolic tangent function returns values in the range `[-1, 1]`. This makes it useful for normalizing inputs in certain types of mathematical and machine learning models.

Evolution of JavaScript from ES1 to ES2020

Introduction to HTML DOM Methods in JavaScript

JavaScript Practice Exercise

Understanding Document Object Model (DOM) in JavaScript

Understanding HTTP Requests and Responses

What is Asynchronous JavaScript?

JavaScript Code for Event Handling

Princites

IITM Software Development Cell