Math.atan2() Method with Example in JavaScript

JavaScript | Math.atan2() Method: Here, we are going to learn about the atan2() method of Math class in JavaScript with Examples.
Submitted by Shivang Yadav, on December 25, 2019

JavaScript | Math.atan2() Method

Math.atan2() is a function in math library of JavaScript that is used to find the arctangent of the quotient of its arguments. The method will return the numeric value which is the angle of (x,y) point and x-axis.

Syntax:

    Math.atan2(y,x);

Parameter(s):

  • y,x – represent the y and x co-ordinates on the point.

Return value:

The return type of this method is number, it returns the arctangent of the quotient of its parameter.

Technical Insights:

  • JavaScript version: ECMAScript 1
  • Browser support: Chrome, Internet Explorer, Mozilla, Safari, Opera mini

Values accepted: Integer, floating-point, numeric string.

Invalid Values: non-numeric string, empty variable, empty array all will return NaN(Not a Number).

Example 1: Valid values for the method

console.log(Math.atan2(2, 5));
console.log(Math.atan2(10.98, 2.3));
console.log(Math.atan2(-3.32, -1.23));
console.log(Math.atan2(0, 0));
console.log(Math.atan2("7.18", -9.23));

Output

0.3805063771123649
1.364310108279956
-1.9256000757252791
0
2.4804744828642002

Example 2: Invalid values for the method.

console.log(Math.atan2("IncludeHelp"));
// Output: NaN

console.log(Math.atan2(1 + 5i));
// Output: Uncaught SyntaxError: Invalid or unexpected token

JavaScript Math Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.