W3Schools.com

JavaScript atan() and atan2() Methods

Math Object Reference JavaScript Math Object

Definition and Usage

The atan() method returns the arctangent of a number as a numeric value between -PI/2 and PI/2 radians.

The atan2() method returns the arctangent of the quotient of its arguments. The angle returned is a numeric value between PI and -PI and represents the counterclockwise angle in radians (not degrees) between the positive X axis and the point (x, y).

Note: With atan2(), the y coordinate is passed as the first argument and the x coordinate is passed as the second argument!

Syntax

Math.atan(x)

Math.atan2(y,x)

Parameter Description
x Required. A number
y Required. A number


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The atan() and atan2() methods are supported in all major browsers.


Examples

Example 1 - atan2(y,x)

Assume you had a point with the (x,y) coordinates of (4,8), you could calculate the angle in radians between that point and the positive X axis as follows:

<script type="text/javascript">

document.write(Math.atan2(8,4));

</script>

The output of the code above will be:

1.1071487177940904


Try it yourself »


Example 2 - atan(x)

We can also calculate the arctangent of the ratio of the two coordinates above (8/4 = 2), which will produce the same output:

<script type="text/javascript">

document.write(Math.atan(2));

</script>

The output of the code above will be:

1.1071487177940904

Try it yourself »


Math Object Reference JavaScript Math Object
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$7.95/mo SEO Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Free Website Templates Free CSS Templates
Make Your Own Website
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE