JavaScript Math asin: How it Works with Examples

math.asin in javascript

Math.asin() finds the arc sine of a number in JavaScript. It helps you work with angles from values. You get the result in radians. JavaScript added Math.asin() to make math tasks easier.

It gives you a way to find angles from sine values. You use it in apps that need trigonometry.

What is Math.asin() in JavaScript?

Math.asin() function finds the angle whose sine equals the input. The input must be between -1 and 1. The result comes in radians. It returns NaN if the input is outside the range. This function belongs to the Math object.

The syntax looks like this:

Math.asin(x)
  • x: a number between -1 and 1
  • Returns: the angle in radians

Let me explain the details. JavaScript uses Math.asin() to trace a value back to its angle. It checks the sine table in reverse. The output gives you an angle in the range -π/2 to π/2.

Use it to reverse a sine result. You need it when you already have the sine and want the angle.

Math.asin(0.5)

This code gives the arcsine of 0.5. The result is about 0.5236 radians. JavaScript reads the value, checks where it falls on the sine graph, and returns the matched angle in radians. It gives the only result possible within the set range.

Examples of JavaScript Math.asin()

Find arc sine of 0

Math.asin(0)

This returns 0 because sine of 0 equals 0. The function matches the input to the sine table and gives the angle that fits the sine value exactly.

Find arc sine of 1

Math.asin(1)

This returns about 1.5708 radians. That means the input matches the sine of π/2. It gives the highest possible result that still fits inside the allowed range.

Find arc sine of -1

Math.asin(-1)

This gives about -1.5708. JavaScript matches the input to the lowest sine value and returns the negative angle that fits the result.

Try a value outside the range

Math.asin(2)

This gives NaN because 2 does not match any sine value. JavaScript only accepts values from -1 to 1. It returns NaN for invalid input.

Use with Math.sin to get back the angle

Math.asin(Math.sin(Math.PI / 3))

This returns about 1.0472, which matches the input angle in radians. JavaScript uses Math.sin to get the sine of π/3 then uses Math.asin to return the original angle.

Store result and convert to degrees

let rad = Math.asin(0.5);
let deg = rad * (180 / Math.PI);

This gives the angle in degrees. JavaScript first gets the radians from Math.asin() function. Then it multiplies by 180 and divides by π to convert it.

Browser and JavaScript Version Support

Compatibility Across Browsers

  • Works in Chrome
  • Works in Firefox
  • Works in Safari
  • Works in Edge
  • Works in Opera

All modern browsers support Math.asin(). You can use it without extra setup.

Support in Older JavaScript Versions

  • Available in ECMAScript 1
  • Works in all versions after ES1

Math.asin() function comes from the first version of JavaScript. It has support in all standard JavaScript engines. You can use it in old code or new code without worry.

Wrapping Up

In this article, you learned how to use JavaScript Math.asin().

Here’s a quick recap:

  • Math.asin() finds the arc sine of a number
  • It only works for values between -1 and 1
  • The result comes in radians
  • The return value shows the angle
  • You can use it with Math.sin to reverse values
  • Invalid inputs give NaN
  • You can convert the result to degrees
  • It works in all major browsers
  • It comes from early JavaScript versions

It returns the arc sine of a number in radians. The value must be between -1 and 1. If the number is outside this range, it returns NaN. The angle always falls between -π/2 and π/2.

Can I use Math asin with degrees?

No. Math asin only gives results in radians. If you want degrees, you must convert the output. Multiply the result by 180 / Math.PI to get the angle in degrees.

What happens if the input is not a number?

JavaScript tries to convert the input to a number. If the result is not valid or not in range, Math.asin returns NaN. Always check the input before you call it.

Does Math asin work in all browsers?

Yes. It works in all modern browsers. It also works in older ones that follow ECMAScript 1 or later. You do not need to add anything extra.

How is Math asin different from Math.sin?

Math.sin gives the sine of an angle. Math.asin does the reverse. It takes a sine value and gives the angle. You use them together to go back and forth between angles and sine values.

Similar Reads

JavaScript “use strict” Mode: What It Is and Why You Should Use It

Code mistakes used to slip by without a warning. That made bugs hard to trace and fix. JavaScript "use strict"…

Assignment Operator in JavaScript with Examples

The assignment operator in JavaScript stores and updates values. It helps you hold data in a variable and change it…

Print in JavaScript Console Using Log, Info, Warn, and Error

The JavaScript console is a tool in web browsers that helps you print the result to the web browser. It…

JavaScript Math trunc: How to Removes Decimals

JavaScript gives you the Math.trunc() function to work with decimal numbers. You use it when you want to remove the…

Flatten an Array in JavaScript: Use flat() to Handle Nested Arrays

Arrays often hold other arrays. This happens with API responses, form data, or nested objects. These layers add extra steps…

JavaScript Ninja Code for Beginners

JavaScript Ninja Code points to ways that help a person write code that runs fast and stays easy to read.…

How to Use the Ternary Operator in JavaScript

The ternary operator in JavaScript keeps your code short. It helps you write conditions inside one line. JavaScript lets you…

JavaScript toString Function: How it Works with Examples

The toString function in JavaScript converts a value to a string form without change. It works on numbers, arrays, and…

Understanding JavaScript Arithmetic Operators

JavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or…

What Is AJAX? How It Works in Web Development with Examples

AJAX keeps your web app fast. You do not need to reload the page every time you send or get…

Previous Article

PHP For Loop: Run Code Using a Counter with Examples

Next Article

PHP Ternary Operator: How to Write Short Expressions

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.