Javascript – Math

Đối tượng toán học cung cấp cho bạn các thuộc tính và phương thức cho các hàm và hằng số toán học. Không giống như các đối tượng toàn cục khác, Math không phải là hàm tạo. Tất cả các thuộc tính và phương thức của Math là tĩnh và có thể được gọi bằng cách sử dụng Math làm đối tượng mà không cần tạo nó.

Vì vậy, bạn gọi hằng số pi là Math.PI và bạn gọi hàm sin là Math.sin(x), trong đó x là đối số của phương thức.

Cú pháp

Cú pháp gọi các thuộc tính và phương thức của Math như sau

var pi_val = Math.PI;
var sine_val = Math.sin(30);

Thuộc tính Math

Sr.No.Property & Description
1E \ Euler’s constant and the base of natural logarithms, approximately 2.718.
2LN2 Natural logarithm of 2, approximately 0.693.
3LN10 Natural logarithm of 10, approximately 2.302.
4LOG2E Base 2 logarithm of E, approximately 1.442.
5LOG10E Base 10 logarithm of E, approximately 0.434.
6PI Ratio of the circumference of a circle to its diameter, approximately 3.14159.
7SQRT1_2 Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
8SQRT2 Square root of 2, approximately 1.414.

Phương thức Math

Sr.No.Method & Description
1abs() Returns the absolute value of a number.
2acos() Returns the arccosine (in radians) of a number.
3asin() Returns the arcsine (in radians) of a number.
4atan() Returns the arctangent (in radians) of a number.
5atan2() Returns the arctangent of the quotient of its arguments.
6ceil() Returns the smallest integer greater than or equal to a number.
7cos() Returns the cosine of a number.
8exp() Returns EN, where N is the argument, and E is Euler’s constant, the base of the natural logarithm.
9floor() Returns the largest integer less than or equal to a number.
10log() Returns the natural logarithm (base E) of a number.
11max() Returns the largest of zero or more numbers.
12min() Returns the smallest of zero or more numbers.
13pow() Returns base to the exponent power, that is, base exponent.
14random() Returns a pseudo-random number between 0 and 1.
15round() Returns the value of a number rounded to the nearest integer.
16sin() Returns the sine of a number.
17sqrt() Returns the square root of a number.
18tan() Returns the tangent of a number.
19toSource() Returns the string “Math”.