AppDividend
Latest Code Tutorials

JavaScript Math sin() Function Example

0

Javascript Math sin() is an inbuilt function that finds the sine value of a given argument. The Math.sin() method comes handy in programming contexts dealing with any trigonometric expressions. The sin() is a static method of Math, and it can be used without creating an object.

JavaScript Math sin()

The Mathsin() function in Javascript is used to return the sine of the number. The sin() method returns the numeric value between -1 and 1, which represents the sine of the angle given in radians. Javascript sin() is a static method of Math; therefore, it is always used as Math.

Syntax

Math.sin(x)

Parameter(s)

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

Return Value

The sine value between -1 and 1.

 

JavaScript Math sin()

Note

  • If the passed value is empty, the sin() method returns NaN.

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

Consider the following examples.

JavaScript Math sin() function example

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

// app.js

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

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

Example 2

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

// app.js

let x;

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

Output

node app
NaN
NaN

Example 3

The following code example demonstrates an application of the Math.sin() method in the simple programming context.

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

// app.js

// 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:
->node example3
Base side: 6
Base angle(in radians): 0.523599
The three sides of the triangle are:
6
6.928204127882605
3.464103410351597

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

See also

Javascript Math exp()

Javascript Math pow()

Javascript Math tanh()

Javascript Math sinh()

Javascript Math tan()

Leave A Reply

Your email address will not be published.

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