JavaScript sin

The JavaScript sin function is a Math function that calculates the trigonometric sine value for the specified expression in radians.

The syntax of the sin function:

Math.sin(number);

Parameter: The number represents the angle in radians.

Return Value: The sin function returns the sine value between -1 and 1. For Infinity and NaN values, it returns the NaN as the output.

The mathematical formula of the JavaScript Trigonometry Sine function:

sin(x) = Length of the Opposite Side / Length of the Hypotenuse

JavaScript sin Function Example

In this example, we will find the Sine values of different data types and display the output—the first four statements concern both positive and negative values.

For Str, we tried this method on the string value. It will return NaN (Not a Number). Next, we tried this Math function on the Null value, which returns zero as output. In the last statement, we used it on multiple values.

TIP: Please refer to the asin, sinh(), and asinh() methods to find the JavaScript Arc Sine and hyperbolic sine values.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptSINFunction </title>
</head>
<body>
  <h1> JavaScriptSINFunction </h1>
  <p id = "Pos"></p>
  <p id = "Neg"></p>
  <p id = "Dec"></p>
  <p id = "Neg_Dec"></p>
  <p id = "Str"></p>
  <p id = "Exp"></p>
  <p id = "Null"></p>
  <p id = "Multi"></p>
  <script>
    document.getElementById("Pos").innerHTML = Math.sin(20);
    document.getElementById("Neg").innerHTML = Math.sin(-10);
    document.getElementById("Dec").innerHTML = Math.sin(40.45);
    document.getElementById("Neg_Dec").innerHTML = Math.sin(-60.75);
    document.getElementById("Str").innerHTML = Math.sin("JavaScript");
    document.getElementById("Null").innerHTML = Math.sin(null);
    document.getElementById("Multi").innerHTML = Math.sin(25 + 55 - 77);
  </script>
</body>
</html>
JavaScript sin Function example

Example 2: In this example, we use the Math.sin() function on positive and negative infinity and the NaN value.

let a = Infinity;
let b = -Infinity;
let c = NaN;
console.log(Math.sin(a));
console.log(Math.sin(b));
console.log(Math.sin(c));
NaN 
NaN 
NaN

Example 3: If we use the sin() function to find the trigonometric sine value of positive and negative 0, it returns 0 and -0 as output.

let a = 0;
let b = -0;
console.log(Math.sin(a));
console.log(Math.sin(b));
0
-0

Example 4: Using the Math.sin() function on PI values.

console.log(Math.sin(Math.PI));
console.log(Math.sin(Math.PI / 2));
1.2246467991473532e-16 
1

Example 5: In the following example, we use the sin() function to find the sine value of negative numbers.

let a = -2;
let b = -3;
console.log(Math.sin(a));
console.log(Math.sin(b));
-0.9092974268256817 
-0.1411200080598672

JavaScript sin degrees

By default, the Math.sin() calculates the sine value of radians, so to calculate the sine value of an angle given in degrees, we must convert degrees to radians.

function calcsD(degrees) {
return Math.sin(degrees * Math.PI / 180);
}

console.log(calcsD(30));
console.log(calcsD(45));
console.log(calcsD(60));
console.log(calcsD(90));
0.49999999999999994 
0.7071067811865475 
0.8660254037844386 
1