Recent Posts
Recent Comments
게으른개발너D
[Math] 까먹는 것 정리 본문
Math
Method
1. Math.sqrt( )
숫자의 제곱근 반환
Math.sqrt(4) // 2
Math.sqrt(2) // 1.414213562373095
Math.sqrt(0) // 0
Math.sqrt(-3) // NaN
2. Math.random( )
0 이상 1 미만의 구간에서 랜덤 수 생성
// 3이상 64이하의 랜덤 숫자 생성
const min = 3;
const max = 64;
const num = Math.floor(Math.random() * (max - min + 1) + min);
3. Math.trunc( )
주어진 값의 소수부분을 제거하고 숫자의 정수부분을 반환.
Math.floor()를 쓸 수도 있지만 숫자가 마이너스 값일 경우 소수부분을 제거한 값과 다른 값이 나오기때문에 Math.trunc()를 사용하는 것이 좋다.
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
cf) Math.floor(): 내림
~~: 내림 연산자
4. Math.sign( )
주어진 수의 부호 (+, -)를 나타내는 함수
+일 경우 1을, -일 경우 -1을, 0일 경우 0을, -0일 경우 -0을 반환한다.
Math.sign(3); // 1
Math.sign(-3); // -1
Math.sign('-3'); // -1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
Math.sign('foo'); // NaN
Math.sign(); // NaN
'개발 > JavaScript' 카테고리의 다른 글
[String] 까먹는 것 정리 (0) | 2023.07.12 |
---|---|
[Object] 까먹는 것 정리 (0) | 2023.07.12 |
[Number] 까먹는 것 정리 (0) | 2023.07.12 |
[Built-in Objects] 까먹는 것 정리 (0) | 2023.07.12 |
Classes - class, extends, super, this (0) | 2023.04.27 |
Comments