The syntax of the Math.sin()
function is:
Math.sin(x)
sin()
, being a static method, is called using the Math
class name.
Math.sin() Parameters
The Math.sin()
function takes in:
- x - A number (in radians) whose sine value is required.
Return value from Math.sin()
- Returns the sine of a given angle (a numeric value between -1 and 1).
Example 1: Using Math.sin()
// sine of 1 radian
var value1 = Math.sin(1);
console.log(value1); // Output: 0.8414709848078965
// negative radians are allowed
var value2 = Math.sin(-2);
console.log(value2); // Output: -0.9092974268256817
// Math constants can be used
var value3 = Math.sin(Math.PI / 2);
console.log(value3); // Output: 1
Output
0.8414709848078965 -0.9092974268256817 1
Example 2: Using Math.sin() with degrees
// custom function for angle in degrees
function sin(degrees) {
var radians = (degrees * Math.PI) / 180;
return Math.sin(radians);
}
// sine of 57 degrees
value1 = sin(57);
console.log(value1); // Output: 0.8386705679454239
// sine of negative degrees
value2 = sin(-90);
console.log(value2); // Output: -1
value3 = sin(90);
console.log(value3); // Output: 1
Output
0.8386705679454239 -1 1
Here, we have defined a sin()
function that converts the degree value to radian and then passes it to Math.sin()
.
We can define custom functions in similar ways to extend the capabilities of such built-in functions.
Recommended readings: