본문 바로가기
FrontEnd/JavaScript

[ JavaScript ] Math.

by ウリ김영은 2023. 10. 24.

< 자주 볼 메소드들 >

  • Math.abs() : 절댓값 반환
  • Math.ceil() : 소수점 올림(큰 정수 반환)
  • Math.floor() : 소수점 내림(작은 정수 반환)
  • Math.round() : 반올림한 가장 가까운 정수 반환
  • Math.trunc() : 소숫점 버리고 정수 반환
Math.ceil(-7.004) // -7
Math.floor(-5.05) //-6
Math.round(-5.05) //-5
Math.round(-5.5) // -5
Math.trunc(-0.123) //-0
  • Math.log() : base e , 0이상이면 -Infinity 반환, 음수면 NaN 반환
  • Math.log10() : base 10 , 음수면 NaN 반환
  • Math.log2 : base 2
Math.log10(100) //2
Math.log10(-2) // NaN
Math.log10(0) // -Infinity
Math.log2(8) // 3
Math.log2(1) // 0
Math.log2(0) // -Infinity
  • Math.max() : 가장 큰 숫자 반환, 파라미터 없으면 -Infinity 반환
  • Math.min() : 가장 작은 숫자 반환, 파라미터 없으면 Infinity 반환
Math.max(-1,-3,-2) // -1
const array1 = [1,3,2]
Math.max(...array1) // 3
  • Math.pow(base, exponent) = base**exponent
  • Math.sqrt() : 제곱근 반환, 음수면 NaN 반환 (square root of a number)
Math.pow(4,0.5) //2
Math.pow(7,-2) // 1/49
Math.pow(-7,0.5) //NaN
Math.sqrt(-1) //NaN
Math.sqrt(-0) // -0
Math.sqrt(9) //3
  • Math.random() : 0이상 1미만의 부동소숫점 의사 난수 반환

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

https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey

https://react.dev/learn/rendering-lists#where-to-get-your-key