Visitar URL original
JavaScript Math.sin() Method
Selected Reading

JavaScript Math.sin() Method



The Math.sin() method in JavaScript is used to calculate the sune value of the provided number (in radians). This method returns a "numeric value" that represents sine value of a given angle (in radians). If the provided argument to this method is NaN or Infinity values, it returns NaN as result.

Syntax

Following is the syntax of JavaScript Math.sin() method −

Math.sin(x)

Parameters

This method accepts only one parameter. The same is described below −

  • x: A numeric value representing angle in radians.

Return value

This method returns a numeric value (between -1 and 1, inclusive) representing the sine of the specified angle.

Example 1

In the following example, we are using the JavaScript Math.sin() method to calculate the sine value of 2 radians −

<html>
<body>
<script>
   const result = Math.sin(2);
   document.write(result);
</script>
</body>
</html>

Output

If we execute the above program, it will return approximately "0.9092".

Example 2

Here, we are computing the sine value of negative 5 radians −

<html>
<body>
<script>
   const result = Math.sin(-5);
   document.write(result);
</script>
</body>
</html>

Output

It will return approximately 0.95892 as result.

Example 3

In the below example, we are computing the sine value of math constant "PI" −

<html>
<body>
<script>
   const result = Math.sin(Math.PI); //3.14
   document.write(result);
</script>
</body>
</html>

Output

The output 1.2246467991473532e-16 represents -1.2246467991473532 × 10-16

Example 4

If we try to calculate the sine value of a "string", this method will return NaN as result −

<html>
<body>
<script>
   const result = Math.sin("Tutorialspoint");
   document.write(result);
</script>
</body>
</html>

Output

As we can see in the output below, it returned NaN as result.

Example 5

The Math.sin() method doesn't treat "Infinity" as a number, so if we pass this as an argument, this method returns NaN as result −

<html>
<body>
<script>
   const result1 = Math.sin(Infinity);
   const result2 = Math.sin(-Infinity);
   document.write(result1, "<br>", result2);
</script>
</body>
</html>

Output

As we can see in the output below, it returned NaN for both positive and negative Infinity.

Advertisements