본문 바로가기
Web/JavaScript

[JavaScript] Math Object

by llHoYall 2022. 7. 8.

Math is a standard built-in object that has only static properties and static methods for mathematical constants and functions.

It works with the Number type and doesn't work with the BigInt type.

Math Property

E

This property is Euler's constant.

Math.E; // 2.718281828459045

LN2

This property is a natural logarithm of 2.

Math.LN2; // 0.6931471805599453

LN10

This property is a natural logarithm of 10.

Math.LN10; // 2.302585092994046

LOG2E

This property is a base-2 logarithm of E.

Math.LOG2E; // 1.4426950408889634

LOG10E

This property is a base-10 logarithm of E.

Math.LOG10E; // 0.4342944819032518

PI

This is a ratio of a circle's circumference to its diameter.

Math.PI; // 3.141592653589793

SQRT1_2

This property is a square root of 1/2.

Math.SQRT1_2; // 0.7071067811865476

SQRT2

This property is a square root of 2.

Math.SQRT2; // 1.4142135623730951

Math Method

abs()

This method returns the absolute value of an argument.

Math.abs(-1); // 1
Math.abs('-1'); // 1
Math.abs(''); // 0
Math.abs([]); // 0
Math.abs(null); // 0
Math.abs(); // NaN
Math.abs(undefined); // NaN
Math.abs({}); // NaN
Math.abs('test'); // NaN

acos()

This method returns the arccosine of an argument.

Math.acos(); // NaN
Math.acos(0.5); // 1.0471975511965979
Math.acos(2); // NaN

acosh()

This method returns the hyperbolic arccosine of an argument.

Math.acosh(); // NaN
Math.acosh(2); // 1.3169578969248166
Math.acosh(Infinity); // Infinity

asin()

This method returns the arcsine of an argument.

Math.asin(); // NaN
Math.asin(1); // 1.5707963267948966
Math.asin(2); // NaN

asinh()

This method returns the hyperbolic arcsine of an argument.

Math.asinh(); // NaN
Math.asinh(2); // 1.4436354751788103
Math.asinh(Infinity); // Infinity

atan()

This method returns the arctangent of an argument.

Math.atan(); // NaN
Math.atan(2); // 1.1071487177940904
Math.atan(Infinity); // 1.5707963267948966

atanh()

This method returns the hyperbolic arctangent of an argument.

Math.atanh(); // NaN
Math.atanh(0.5); // 0.5493061443340548
Math.atanh(1); // Infinity
Math.atanh(2); // NaN

atan2()

This method returns the arctangent of the quotient of its arguments.

The first argument is a coordinate of y and the second argument is a coordinate of x.

Math.atan2(); // NaN
Math.atan2(1); // NaN
Math.atan2(1, 2); // 0.4636476090008061
Math.atan2(2, 1); // 1.1071487177940904

cbrt()

This method returns the cube root of an argument.

Math.cbrt(); // NaN
Math.cbrt(8); // 2

ceil()

This method returns the smallest integer greater than or equal to an argument.

Math.ceil(); // NaN
Math.ceil(1.1); // 2
Math.ceil(-1.1); // -1

clz32()

This method returns the number of leading zero bits of the 32-bit integer argument.

Math.clz32(); // 32
Math.clz32(1023); // 22

cos()

This method returns the cosine of an argument.

Math.cos(); // NaN
Math.cos(1); // 0.5403023058681398
Math.cos(Infinity); // NaN

cosh()

This method returns the hyperbolic cosine of an argument.

Math.cosh(); // NaN
Math.cos(1); // 1.5430806348152437
Math.cos(Infinity); // Infinity

exp()

This method returns (e^argument).

Math.exp(); // NaN
Math.exp(0); // 1
Math.exp(1); // 2.718281828459045

expm1()

This method returns by subtracting 1 from exp().

Math.expm1(); // NaN
Math.expm1(0); // 0
Math.expm1(1); // 1.718281828459045

floor()

This method returns the largest integer less than or equal to an argument.

Math.floor(); // NaN
Math.floor(1.9); // 1
Math.floor(-1.1); // -2

fround()

This method returns the nearest single-precision float representation of an argument.

Math.fround(); // NaN
Math.fround(1.1234); // 1.1233999729156494
Math.fround(-1.4321); // -1.4321000576019287

hypot()

This method returns the square root of the sum of squares of its arguments.

Math.hypot(); // 0
Math.hypot(1, 2); // 2.23606797749979 <- (1^2 + 2^2)^(1/2)

imul()

This method returns the result of the 32-bit integer multiplication of two arguments.

Math.imul(); // 0
Math.imul(1); // 0
Math.imul(1, 2); // 2 <- 1 * 2
Math.imul(2, 3); // 6 <- 2 * 3

log()

This method returns the natural logarithm of an argument.

Math.log(); // NaN
Math.log(0); // -Infinity
Math.log(1); // 0
Math.log(Infinity); // Infinity

log1p()

This method returns the natural logarithm of (1 + argument) for an argument.

Math.log1p(); // NaN
Math.log1p(0); // 0 <- ln(0 + 1)
Math.log1p(1); // 0.6931471805599453 <- ln(1 + 1)
Math.log1p(Infinity); // Infinity

log10()

This method returns the base-10 logarithm of an argument.

Math.log10(); // NaN
Math.log10(0); // -Infinity
Math.log10(1); // 0
Math.log10(Infinity); // Infinity

log2()

This method returns the base-2 logarithm of an argument.

Math.log2(); // NaN
Math.log2(0); // -Infinity
Math.log2(1); // 0
Math.log2(Infinity); // Infinity

max()

This method returns the largest of zero or more numbers.

Math.max(); // -Infinity
Math.max(1, 2, 3); // 3
Math.max([1, 2, 3]); // NaN
Math.max(...[1, 2, 3]); // 3

min()

This method returns the smallest of zero or more numbers.

Math.min(); // Infinity
Math.min(1, 2, 3); // 1
Math.min([1, 2, 3]); // NaN
Math.min(...[1, 2, 3]); // 1

pow()

This method returns the first argument as a base to the second argument as an exponent power.

Math.pow(); // NaN
Math.pow(2); // NaN
Math.pow(2, 2); // 4 <- 2^2
Math.pow(3, 2); // 9 <- 3^2
Math.pow(4, -1); // 0.25 <- 1/4 <- 4^(-1)

The exponential operator introduced in ES7 is better in terms of readability.

2 ** 3; // 8 <- 2^3

random()

This method returns a pseudo-random number between 0 and 1.

Math.random(); // 0.33560729376448073

round()

This method returns the value of an argument rounded to the nearest integer.

Math.round(); // NaN
Math.round(1.4); // 1
Math.round(1.5); // 2
Math.round(-1.5); // -1
Math.round(-1.6); // -2

sign()

This method returns the sign of an argument, indicating whether an argument is positive, negative, or zero.

Math.sign(); // NaN
Math.sign(3); // 1
Math.sign(0); // 0
Math.sign(-2); // -1

sin()

This method returns the sine of an argument.

Math.sin(); // NaN
Math.sin(1); // 0.8414709848078965
Math.sin(Infinity); // NaN

sinh()

This method returns the hyperbolic sine of an argument.

Math.sinh(); // NaN
Math.sinh(1); // 1.1752011936438014
Math.sinh(Infinity); // Infinity

sqrt()

This method returns the positive square root of an argument.

Math.sqrt(); // NaN
Math.sqrt(4); // 2
Math.sqrt(-4); // NaN

tan()

This method returns the tangent of an argument.

Math.tan(); // NaN
Math.tan(1); // 1.5574077246549023
Math.tan(Infinity); // NaN

tanh()

This method returns the hyperbolic tangent of an argument.

Math.tanh(); // NaN
Math.tanh(1); // 0.7615941559557649
Math.tanh(Infinity); // 1

trunc()

This method returns the integer portion of an argument, removing any fractional digits.

Math.trunc(); // NaN
Math.trunc(12.345); // 12
Math.trunc(-12.345); // -12

'Web > JavaScript' 카테고리의 다른 글

[JavaScript] RegExp Object  (0) 2022.07.09
[JavaScript] Date Object  (0) 2022.07.09
[JavaScript] Number Object  (0) 2022.07.07
[JavaScript] Array  (0) 2022.07.06
[JavaScript] Class  (0) 2022.07.01

댓글