29장 Math
- Math는 수학적인 상수와 함수를 위한 프로퍼티와 메서드 제공
- 생성자 함수가 아니므로, 정적 프로퍼티와 정적 메서드만 제공
1. Math 프로퍼티
1.1 Math.PI
2. Math 메서드
2.1 Math.abs
- 인자로 전달된 숫자의 절대값 반환
- 절대값은 반드시 0 또는 양수여야 한다.
Math.abs(-1);
Math.abs('-1');
Math.abs('');
Math.abs([]);
Math.abs(null);
Math.abs(undefined);
Math.abs({});
Math.abs('string');
Math.abs();
2.2 Math.round
- 인자로 전달된 숫자의 소수점 이하를 반올림한 정수를 반환
Math.round(1.4);
Math.round(1.6);
Math.round(-1.4);
Math.round(-1.6);
Math.round(1);
Math.round();
2.3 Math.ceil
Math.ceil(1.4);
Math.ceil(1.6);
Math.ceil(-1.4);
Math.ceil(-1.6);
Math.ceil(1);
Math.ceil();
2.4 Math. floor
Math.floor(1.9);
Math.floor(9.1);
Math.floor(-1.9);
Math.floor(-9.1);
Math.floor(1);
Math.floor();
2.5 Math.sqrt
Math.sqrt(9);
Math.sqrt(-9);
Math.sqrt(2);
Math.sqrt(1);
Math.sqrt(0);
Math.sqrt();
2.6 Math.random
Math.random();
const random = Math.floor((Math.random() * 10) + 1);
console.log(random);
2.7 Math.pow
- 첫번째 인수를 밑(base)으로, 두번째 인수를 지수(exponent)로 거듭제곱한 결과를 반환
Math.pow(2, 8);
Math.pow(2, -1);
Math.pow(2);
2 ** 2 ** 2;
Math.pow(Math.pow(2, 2), 2);
2.8 Math.max
- 전달받은 인수 중 가장 큰 수 반환
- 인수가 없다면 -Infinity
Math.max(1);
Math.max(1, 2);
Math.max(1, 2, 3);
Math.max();
Math.max.apply(null, [1,2,3]);
Math.max(...[1, 2, 3]);
2.9 Math.min
- 전달받은 인수 중 가장 작은 수 반환
- 인수가 없다면 Infinity
Math.min(1);
Math.min(1, 2);
Math.min(1, 2, 3);
Math.min();
Math.max.apply(null, [1,2,3]);
Math.min(...[1, 2, 3]);