The sin()
method computes the trigonometric sine of the specified angle and returns it.
Example
// sine of the angle 1
var value1 = Math.sin(1);
console.log(value1);
// Output: 0.8414709848078965
sin() Syntax
The syntax of the Math.sin()
method is:
Math.sin(angle)
Here, sin()
is a static method. Hence, we are accessing the method using the class name, Math
.
sin() Parameter
The sin()
method takes a single parameter:
angle
- in radians whose sine value is to be calculated
sin() Return Value
The sin()
method returns:
- sine value of a given
angle
(in radians) - NaN (Not a Number) for a non-numeric argument
Example 1: JavaScript Math.sin()
// sine of the angle 5
let value1 = Math.sin(5);
console.log(value1);
// negative angles are allowed
let value2 = Math.sin(-2);
console.log(value2);
// Output:
// -0.9589242746631385
// -0.9092974268256817
In the above example,
Math.sin(5)
- computes the sine value of the angle 5Math.sin(-2)
- computes the sine value of the angle -2
Example 2: Math.sin() with Math Constants
// math constants can be used
let value = Math.sin(Math.PI);
console.log(value);
// Output: 1.2246467991473532e-16
In the above example, we have used the sin()
method to compute the sine of the math constant PI
.
Here, the output -1.2246467991473532e-16 represents -1.2246467991473532 * 10-16
Example 3: Math.sin() with Non-Numeric Arguments
let string = "David"
// sin() with string as argument
let value = Math.sin(string);
console.log(value);
// Output: NaN
In the above example, we have tried to calculate the sine value of the string "David"
. Hence, we get NaN as the output.
Example 4: Math.sin() with Infinity argument
// infinity as argument
let value1 = Math.sin(Infinity);
console.log(value1);
// negative infinity as argument
let value2 = Math.sin(-Infinity);
console.log(value2);
// Output:
// NaN
// NaN
The sin()
method doesn't treat infinity as a number so the method returns NaN with this argument.
Also, the sine of an infinite angle is indefinite, which can't be defined with a number.
Recommended readings: