/

HTML / CSS / JavaScript Tutorial

JavaScript Math method: atan2()

[this page | pdf | back links]

The atan2() method (of the Math object) returns the (principal) arctangent of a real number.

 

It has the following syntax with the following parameters:

 

Math.atan2(y,x)

 

Parameter

Required / Optional

Description

y

Required

y-coordinate

x

Required

x-coordinate

 

Note: many computer languages have an atan2 function, but the ordering of the parameters is not the same across all languages

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var b = -0.2;
var b2 = 0.3;
document.getElementById("Example").innerHTML =
  'var x = Math.atan2(' + b + ', ' + b2 + ')';
document.getElementById("Result").innerHTML = Math.atan2(b,b2);
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodMathAtan2() {
  return !!Math.atan2(12.5, 0.5);
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Numbers and Math


Desktop view | Switch to Mobile