totn JavaScript

JavaScript: Math acos() function

This JavaScript tutorial explains how to use the math function called acos() with syntax and examples.

Description

In JavaScript, acos() is a function that is used to return the arc cosine (also called inverse cosine) of a number, with the result expressed in radians. Because the acos() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

Syntax

In JavaScript, the syntax for the acos() function is:

Math.acos(number);

Parameters or Arguments

number
The number used to calculate the arc cosine in radians. The number must be a value between 1 and -1.

Returns

The acos() function returns the arc cosine of a number and the result is expressed in radians.

If the number is not within the range of -1 and 1, the acos() function will return NaN.

Note

  • Math is a placeholder object that contains mathematical functions and constants of which acos() is one of these functions.

Example

Let's take a look at an example of how to use the acos() function in JavaScript.

For example:

console.log(Math.acos(0.75));
console.log(Math.acos(-0.9));
console.log(Math.acos(2));

In this example, we have invoked the acos() function using the Math class.

We have written the output of the acos() function to the web browser console log, for demonstration purposes, to show what the acos() function returns.

The following will be output to the web browser console log:

0.7227342478134157
2.6905658417935308
NaN

In this example, the first output to the console log returned 0.7227342478134157 which is the arc cosine of 0.75. The second output to the console log returned 2.6905658417935308 which is the arc cosine of -0.9. The third output to the console log returned NaN since the number was not within the range of 1 and -1.

The result from the acos() function is expressed in radians.