JavaScript asin: How to Use Math.asin() in Javascript
The asin() method comes in handy in programming contexts dealing with trigonometric expressions.
JavaScript Math.asin
Javascript Math asin() is a built-in function used to find the arcsine value in radians of a given argument. The Math.asin() function returns a numeric value between -pi/2 and pi/2 radians. Javascript asin() is a static method of Math. So, it is always used as Math.asin(), rather than as a method of the Math object.
To find the arcsine value of a given argument in JavaScript, you can use the Math.asin() method.
Syntax
Math.asin(x)
Parameters
The variable x lies between -1 and 1, whose arcsine value is to be determined.
Return Value
The arcsine value between -pi/2 and pi/2 radians.
See the following figure.
Note:
- If the passed value lies outside the range [-1,1], the method returns NaN.
Compatibility
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
- Android webview
- Chrome for Android
- Firefox for Android
- Opera for Android
- Safari on iOS
- Samsung Internet
- Node.js
JavaScript version: ECMAScript 1
Consider the following examples
JavaScript Math asin() Code Example
See the following code.
// example1.js var a = -1; var b = 1; var c = 0.5; var d = 0; console.log(Math.asin(a)); console.log(Math.asin(b)); console.log(Math.asin(c)); console.log(Math.asin(d));
Output
node example1 -1.5707963267948966 1.5707963267948966 0.5235987755982989 0
Example 2
The following example demonstrates the case where values outside the range of [-1,1] are passed as parameters.
// example2.js var a = 2; var b = -2; console.log(Math.asin(a)); console.log(Math.asin(b));
Output
node example2 NaN NaN
Example 3
The following code example demonstrates the application of this method in a simple programming context.
Given the three sides of a right-angled triangle, find all of its angles.
// example3.js var h; var b; var p; const r = require('readline'); const rl = r.createInterface({ input: process.stdin, output: process.stdout }); const prompt1 = () => { return new Promise((resolve, reject) => { rl.question('Hypotenuse: ', (answer) => { h = answer; resolve(); }); }); }; const prompt2 = () => { return new Promise((resolve, reject) => { rl.question('Base: ', (answer) => { b = answer; resolve(); }); }); }; const prompt3 = () => { return new Promise((resolve, reject) => { rl.question('Perpendicular: ', (answer) => { p = answer; resolve(); }); }); }; const main = async () => { await prompt1(); await prompt2(); await prompt3(); rl.close(); console.log('The three angles of the triangle are(in radians):'); console.log(Math.asin(1)); //pi/2 radians console.log(Math.asin(p / h)); //base angle console.log(Math.asin(1) - Math.asin(p / h)); //third angle } main();
Output
Test Case 1: ->node example3 Hypotenuse: 6.928 Base: 6 Perpendicular: 3.464 The three angles of the triangle are: 1.5707963267948966 0.5235987755982989 1.0471975511965976 Test Case2: ->node example3 Hypotenuse: 42.426 Base: 30 Perpendicular: 30 The three angles of the triangle are(in radians): 1.5707963267948966 0.7854077535813886 0.785388573213508