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.
Table of Content
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)xis 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?
Can I use Math.sin with degrees?
Why does Math.sin(Math.PI) not return exactly 0?
What happens if I give a string to Math.sin?
Is Math.sin accurate for large numbers?
Similar Reads
You need to compare values in almost every JavaScript program. This is where comparison operators come in. They check if…
JavaScript optional chaining lets you access object values without runtime errors. It checks if a property exists before access and…
The assignment operator in JavaScript stores and updates values. It helps you hold data in a variable and change it…
JavaScript has a statement called "with" that changes how code accesses object properties. The"with" Function makes a temporary scope, so…
JavaScript object methods are simple ways to handle data inside objects. An object can hold many values, and methods give…
JavaScript added Math.cbrt to solve cube roots. Before that, developers used custom code or Math.pow with 1/3. It works with…
JavaScript removes unused memory blocks with garbage collection. It frees memory space so programs run without leaks. What is JavaScript…
JavaScript gives you the Math.trunc() function to work with decimal numbers. You use it when you want to remove the…
If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…
You use loops in JavaScript to repeat code without copying it many times. This helps when you handle lots of…