솔미는 성장중

[JS] 표준 내장 객체 : 수학 본문

JavaScript/표준 내장 객체

[JS] 표준 내장 객체 : 수학

solming 2023. 7. 20. 14:11
728x90

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

 

Math - JavaScript | MDN

The Math namespace object contains static properties and methods for mathematical constants and functions.

developer.mozilla.org


Math.abs()

주어진 숫자의 절댓값을 반환

Math.abs(-Infinity); // Infinity
Math.abs(-1); // 1
Math.abs(-0); // 0
Math.abs(0); // 0
Math.abs(1); // 1
Math.abs(Infinity); // Infinity

Math.ceil()

주어진 숫자를 올림해 정수를 반환

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

Math.floor()

주어진 숫자를 내림해 정수를 반환

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

Math.round()

주어진 숫자를 반올림해 정수를 반환

Math.round(-Infinity); // -Infinity
Math.round(-20.51); // -21
Math.round(-20.5); // -20
Math.round(-0.1); // -0
Math.round(0); // 0
Math.round(20.49); // 20
Math.round(20.5); // 21
Math.round(42); // 42
Math.round(Infinity); // Infinity

 

 

Math.max()

주어진 숫자 중 가진 큰 숫자를 반환

Math.min()

주어진 숫자 중 가진 작은 숫자를 반환

 

Math.pow()

주어진 숫자의 거듭제곱한 값을 반환

Math.pow(base, exponent)

console.log(Math.pow(7, 3));
// Expected output: 343

console.log(Math.pow(4, 0.5));
// Expected output: 2

console.log(Math.pow(7, -2));
// Expected output: 0.02040816326530612
//                  (1/49)

console.log(Math.pow(-7, 0.5));
// Expected output: NaN

 

Math.random()

숫자 0 이상, 1미만의 난수 반환

 

0~1사이 랜덤한 수 / 0~10사이 랜덤한 수/ 11~20 랜덤 / 101~999 랜덤

 

 

728x90