JavaScript Math sin() Method

JavaScript Math.sin() is a static function that calculates the sine of a given angle in radians. The sine is a trigonometric function that represents the ratio of the opposite side’s length to the hypotenuse length in a right-angled triangle.

Syntax

Math.sin(x)

Parameter(s)

The variable x (in radians), whose sine value is to be determined.

Return Value

The sine value is between -1 and 1.

JavaScript Math sin()

Note

  • The sin() method returns NaN if the passed value is empty.

 

Example 1

The following code demonstrates the use of the sin() method.

let a = 0;
let b = 1;
let c = Math.PI;
let d = 2 * Math.PI;
let e = (Math.PI) / 2;

console.log(Math.sin(a));
console.log(Math.sin(b));
console.log(Math.sin(c));
console.log(Math.sin(d));
console.log(Math.sin(e));

Output

0
0.8414709848078965
1.2246467991473532e-16
-2.4492935982947064e-16
1

Example 2

The following example demonstrates the case where an empty value is passed.

let x;

console.log(Math.sin(x));
console.log(Math.sin());

Output

NaN
NaN

Example 3

The following code example demonstrates applying the Math.sin() method in a simple programming context.

Given the base angle of the triangle and the base side, find all the remaining sides of the triangle.


// Given the base angle of a triangle and the base side,
// find all the remaining sides of the triangle.

let p;
let b;
let h;

let angle;

const r = require('readline');
const rl = r.createInterface({
 input: process.stdin,
 output: process.stdout
});

const prompt1 = () => {
 return new Promise((resolve, reject) => {
 rl.question('Base side: ', (answer) => {
 b = answer;
 resolve();
 });
 });
};

const prompt2 = () => {
 return new Promise((resolve, reject) => {
 rl.question('Base angle(in radians): ', (answer) => {
 angle = answer;
 resolve();
 });
 });
};

const main = async () => {
 await prompt1();
 await prompt2();
 rl.close();

 console.log('The three sides of the triangle are:');
 console.log(b); //base side

 let angle2 = (Math.PI) / 2 - angle;

 h = b / (Math.sin(angle2));
 console.log(h); //hypotenuse

 p = h * Math.sin(angle);

 console.log(p); //third side
}

main();

Output

Test Case 1:
Base side: 6
Base angle(in radians): 0.523599
The three sides of the triangle are:
6
6.928204127882605
3.464103410351597

Test Case2:
Base side: 30
Base angle(in radians): 0.785398
The three sides of the triangle are:
30
42.42639993882793
29.99999019615471

Compatibility(Version and above)

  1. Google Chrome v1
  2. Internet Explorer v3
  3. Firefox v1
  4. Edge v12
  5. Opera v3
  6. Safari v1
  7. Android webview v1 
  8. Chrome for Android v18
  9. Firefox for Android v4
  10. Opera for Android v10.1
  11. Safari on iOS v1
  12. Samsung Internet v1.0
  13. Node.js

JavaScript version: ECMAScript 1

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.