JavaScript Math sin() Method

JavaScript Math sin() is a “static function used to calculate the sine of a number”. The Math.sin() function returns a numeric value between -1 and 1, representing the sine of the angle given in radians. 

Syntax

Math.sin(x)

Parameter(s)

x: It is a number 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: How to Use the sin() method

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: Passing an empty value to the Math sin() function

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: Passing a Pi as an argument

console.log(Math.sin(Math.PI / 2));

Output

1

Example 4: Real-world example

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

Browser Compatibility(Version and above)

  1. Google Chrome 1
  2. Internet Explorer 3
  3. Firefox 1
  4. Edge 12
  5. Opera 3
  6. Safari 1
  7. Android webview 1 
  8. Chrome for Android 18
  9. Firefox for Android 4
  10. Opera for Android 10.1
  11. Safari on iOS 1
  12. Samsung Internet 1.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.