What is Math.atan2() in JavaScript?
Math is a built-in object in JavaScript that contains different methods and properties used to perform mathematical operations. It contains the atan2() function which is used to compute the angle between the positive x-axis and the ray originating from (0,0) to (x,y) points.
Syntax
Math.atan2(y,x);
Parameter
x: It is of typeNumberand indicates the x-coordinate of the input point.y: It is of typeNumberand indicates the y-coordinate of the input point.
Numberin JavaScript is a 64-bit double-precision value that is the same asdoublein C# or Java.
Return value
Angle: It returns the
angle θ between the positive x-axis and the ray starting from point (0,0) to the point (x,y). It is measured in radians. Its range is
-π <= θ <= π (radians)
π is a mathematical constant whose value is equal to 3.14159 approximately.
Code
console.log("atan2(0,0) = " + Math.atan2(0,0));console.log("atan2(-10,-10) = " + Math.atan2(-10,-10));console.log("atan2(10,10) = " + Math.atan2(10,10));console.log("atan2(10,-10) = " + Math.atan2(10,-10));console.log("atan2(-10,10) = " + Math.atan2(-10,10));console.log("atan2(∞,10) = " + Math.atan2(Infinity,10));console.log("atan2(10,-∞) = " + Math.atan2(10,-Infinity));