본문 바로가기
정리_창고

JavaScript - Math Methods 사전식 정리

by SKim입니다 2020. 7. 8.

JavaScript 총정리 - 목차로 바로가기

 

abs()  Math.abs(x)

숫자의 절대값을 리턴한다.

더보기

리턴값: specify된 숫자의 절대값을 다타내는 숫자. 그 값이 숫자가 아니면 NaN을, 그 값이 null이면 0을 리턴한다.

x - 숫자

 

Math.abs('-1');     // 1
Math.abs(-2);       // 2
Math.abs(null);     // 0
Math.abs('');       // 0
Math.abs([]);       // 0
Math.abs([2]);      // 2
Math.abs([1,2]);    // NaN
Math.abs({});       // NaN
Math.abs('string'); // NaN
Math.abs();         // NaN

 

acos()
acosh()
asin()
asinh()
atan()
atan2()
atanh()


cbrt()  Math.cbrt(x)

숫자의 세제곱근(cubic root)을 리턴한다.

더보기

리턴값: 숫자

x - 숫자

 

Math.cbrt(NaN); // NaN
Math.cbrt(-1); // -1
Math.cbrt(-0); // -0
Math.cbrt(-Infinity); // -Infinity
Math.cbrt(0); // 0
Math.cbrt(1); // 1
Math.cbrt(Infinity); // Infinity
Math.cbrt(null); // 0
Math.cbrt(2);  // 1.2599210498948734

 

ceil()  Math.ceil(x)

숫자를 올림한 값을 리턴한다.

더보기

리턴값: 올림한 숫자

x - 올림하고 싶은 숫자

 

Math.ceil(.95);    // 1
Math.ceil(4);      // 4
Math.ceil(7.004);  // 8
Math.ceil(-0.95);  // -0
Math.ceil(-4);     // -4
Math.ceil(-7.004); // -7


cos()
cosh()
E
exp()


floor()  Math.floor(x)

숫자를 내림한 값을 리턴한다.

더보기

리턴값: 내림한 숫자

x - 내림하고 싶은 숫자

 

Math.floor( 45.95); //  45
Math.floor( 45.05); //  45
Math.floor(  4   ); //   4
Math.floor(-45.05); // -46 
Math.floor(-45.95); // -46

 

LN2
LN10
log()
LOG2E
LOG10E

 

max()  Math.max([value1[, value2[, ...]]])

가장 높은 값을 가진 숫자를 리턴한다.

더보기

리턴값: 인자 중 가장 높은 값을 나타내는 숫자. 인자가 하나도 없으면 -Infinity를 리턴한다. 하나 이상의 인자가 숫자가 아니면 NaN을 리턴한다.

value - 비교할 숫자

 

Math.max(10, 20);   //  20
Math.max(-10, -20); // -10
Math.max(-10, 20);  //  20

 

min()  Math.min([value1[, value2[, ...]]])

가장 낮은 값을 가진 숫자를 리턴한다.

더보기

리턴값: 인자 중 가장 낮은 값을 나타내는 숫자. 인자가 하나도 없으면 -Infinity를 리턴한다. 하나 이상의 인자가 숫자가 아니면 NaN을 리턴한다.

value - 비교할 숫자

 

var x = 10, y = -20;
var z = Math.min(x, y);
// -20

 

PI


pow()  Math.pow(base, exponent)

base를 exponent제곱한 값을 리턴한다.

더보기

리턴값: base를 exponent제곱한 값을 나타내는 숫자

base

exponent

 

Math.pow(7, 2);    // 49

 

random()  Math.random()

0 이상 1 미만의 랜덤한 숫자를 리턴한다.

더보기

리턴값: 0 이상 1 미만의 랜덤한 숫자

 

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

console.log(getRandomInt(3));
// expected output: 0, 1 or 2

console.log(getRandomInt(1));
// expected output: 0

console.log(Math.random());
// expected output: a number between 0 and 1

 

round()  Math.round(x)

숫자를 반올림한 값을 리턴한다.

더보기

리턴값: 반올림한 숫자

x - 반올림하고 싶은 숫자

 

console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
// expected output: 6 6 5


sin()

 

sqrt()  Math.sqrt(x)

숫자의 제곱근(square root)을 리턴한다.

더보기

리턴값: 숫자. x가 음수이면 NaN을 리턴한다.

x - 숫자

 

Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

 

SQRT1_2
SQRT2
tan()
tanh()

 

trunc()  Math.trunc(x)

숫자의 정수 부분만을 리턴한다.

더보기

리턴값: 숫자

x - 숫자

 

Math.trunc(13.37);    // 13
Math.trunc(42.84);    // 42
Math.trunc(0.123);    //  0
Math.trunc(-0.123);   // -0
Math.trunc('-1.123'); // -1
Math.trunc(NaN);      // NaN
Math.trunc('foo');    // NaN
Math.trunc();         // NaN

 

 

< 출처 >

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math

 

Math

Math는 수학적인 상수와 함수를 위한 속성과 메서드를 가진 내장 객체입니다.

developer.mozilla.org

https://www.w3schools.com/jsref/jsref_obj_math.asp

 

JavaScript Math Reference

JavaScript Math Reference Math Object The Math object allows you to perform mathematical tasks. Math is not a constructor. All properties/methods of Math can be called by using Math as an object, without creating it: var x = Math.PI;           

www.w3schools.com

 

댓글