JavaScript Math sin Function with Syntax and Uses

Math.sin() in JavaScript gives the sine of a number. This number must be in radians. You use it to work with angles in math problems. JavaScript Math.sin() works well for tasks like animation. You can also use it in rotation or general trigonometry.

It works in all browsers and JavaScript versions. You do not install it. It runs directly from the Math object.

Understand Math.sin() in JavaScript

JavaScript Math.sin() gives the sine value of a number measured in radians. You must give a number as the input. This method comes from the Math object. It works like other built-in math methods.

You call it with one number value. It uses that number as the angle in radians. It returns a number between -1 and 1.

The syntax looks like this:

Math.sin(x)
  • x is a number in radians.
  • The return value is the sine of x.

The method uses the input as an angle in radians. It runs a built-in formula to return the sine. You do not need extra setup.

Use it in math, geometry or animation. Call it when you rotate shapes or compute angles.

let result = Math.sin(Math.PI / 2);

This line sets result to the sine of π divided by 2. JavaScript converts π/2 into a radian angle. Then it returns the sine of that angle. The final result is 1. This shows how to work with radians and get the expected output from the method. It runs from the Math object without extra steps.

Examples of JavaScript Math.sin()

Find sine of 90 degrees

let result = Math.sin(Math.PI / 2);

This example converts 90 degrees to radians using π/2. Then it gets the sine of that angle. The output is 1 because the sine of 90 degrees equals 1 in trigonometry. This shows a basic use case.

Find sine of 0 degrees

let result = Math.sin(0);

This uses 0 as the angle. The result is 0 because the sine of 0 equals 0. It shows how the method handles inputs without conversion.

Find sine of 180 degrees

let result = Math.sin(Math.PI);

This finds the sine of π radians, which equals 180 degrees. The result is close to 0. It may return a small number like 1.2246e-16 due to floating-point limits. This still means 0.

Use a negative angle

let result = Math.sin(-Math.PI / 2);

This uses a negative angle. The sine of -π/2 equals -1. It shows how the method handles negative radians and returns the right result.

Use a large number

let result = Math.sin(20);

This uses a large input. It still returns a value between -1 and 1. The result may not seem obvious. The function keeps working because it repeats for all real numbers.

Use it in a loop

for (let i = 0; i <= Math.PI; i += Math.PI / 4) {
  console.log(Math.sin(i));
}

This loop runs from 0 to π in small steps. It prints the sine of each angle. The output shows how the sine grows and drops as the angle changes. This fits well with charts or graphs.

Browser and JavaScript Version Support

Compatibility Across Browsers

  • Works in Chrome, Firefox, Safari, Edge, and Opera.
  • Runs in all modern browsers without setup.
  • No updates or add-ons needed.

Support in Older JavaScript Versions

  • Available from early ECMAScript versions.
  • No version block or syntax change.
  • It works in ES1, ES3, ES5, and all newer versions.
  • Runs the same on both old and new engines.

Math.sin has full support. You do not worry about browser or version issues.

Wrapping Up

In this article, you learned what JavaScript Math.sin() does and how it returns the sine of a number in radians.

Here’s a quick recap:

  • JavaScript Math.sin() comes from the Math object
  • It needs one number as input
  • The input must be in radians
  • The return value stays between -1 and 1
  • Use it for angles, waves, and rotation
  • It works in all browsers
  • You do not need extra libraries
  • Use it with other Math methods like Math.PI
  • It handles both small and large numbers
  • It runs the same in all JavaScript versions

What does Math.sin return in JavaScript?

Math.sin returns the sine of a number. The input must be in radians. The return value stays between -1 and 1. This lets you work with angles and trigonometry.

Can I use Math.sin with degrees?

No, Math.sin only works with radians. You must convert degrees to radians first. Multiply the degree value by π and divide by 180 to get the right input.

Why does Math.sin(Math.PI) not return exactly 0?

It does not return 0 because of floating-point limits. The result may be a very small number close to 0. This happens with many math methods in JavaScript.

What happens if I give a string to Math.sin?

If you give a string, JavaScript tries to convert it. If the string is not a valid number, it returns NaN. Always use a number to avoid errors.

Is Math.sin accurate for large numbers?

It stays accurate within the limits of floating-point math. For very large values, small errors may appear. The output still stays between -1 and 1.
Previous Article

Math.sign in JavaScript: Check Number Signs

Next Article

JavaScript Math acos Function

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.