Trigonometric Functions: sin, cos, tan
You'll learn how to calculate sine, cosine, and tangent of angles using JavaScript's built-in Math methods.
What Are Trig Functions?
Trigonometric functions help you work with angles and triangles. In JavaScript, the Math object provides three core trig functions: Math.sin(), Math.cos(), and Math.tan(). These are essential for anything involving rotation, waves, circular motion, or geometric calculations.
The Critical Detail: Radians, Not Degrees
Here's what catches most beginners: JavaScript's trig functions expect angles in radians, not degrees. A full circle is 2π radians (about 6.28), which equals 360 degrees.
To convert degrees to radians: multiply by Math.PI / 180.
Using the Functions
// Calculate sine of 90 degrees
const angleInDegrees = 90;
const angleInRadians = angleInDegrees * (Math.PI / 180);
const result = Math.sin(angleInRadians);
console.log(result); // 1
// Or use radians directly
console.log(Math.sin(Math.PI / 2)); // 1 (90 degrees)
console.log(Math.cos(0)); // 1 (0 degrees)
console.log(Math.tan(Math.PI / 4)); // 1 (45 degrees)
Each function returns a number between -1 and 1 (for sin and cos), or any value (for tan):
// Common angles
console.log(Math.sin(0)); // 0
console.log(Math.cos(Math.PI)); // -1
console.log(Math.tan(0)); // 0
Think of it like this: if Math.sqrt() reverses squaring, trig functions help you find relationships in right triangles and circular motion.
Key Takeaway: Use Math.sin(), Math.cos(), and Math.tan() with radian measurements—multiply degrees by Math.PI / 180 to convert first.