본문 바로가기
🌈JAVAScript

[JAVAScript/자바스크립트]수학, 랜덤, 삼항 연산자

by 김말자 2022. 10. 12.
728x90
728x90
BIG

Math

다른 객체와 달리 생성자가 없음

정적임

먼저 생성하지 않고도 모든 메서드의 속성을 사용가능

상수(property)

Math.property

  • Math.E        // returns Euler's number
  • Math.PI       // returns PI
  • Math.SQRT2    // returns the square root of 2
  • Math.SQRT1_2  // returns the square root of 1/2
  • Math.LN2      // returns the natural logarithm of 2
  • Math.LN10     // returns the natural logarithm of 10
  • Math.LOG2E    // returns base 2 logarithm of E
  • Math.LOG10E   // returns base 10 logarithm of E

보통 Math.method(number)로 많이 사용함

 

정수반환

Math.round(x) 가장 가까운 정수로 반올림된 x를 반환합니다.
Math.ceil(x) 가장 가까운 정수로 반올림된 x를 반환합니다.
Math.floor(x) 가장 가까운 정수로 내림한 x를 반환합니다.
Math.trunc(x) x의 정수 부분을 반환합니다( ES6의 새로운 기능 ) .

Math.sign(x) 

x가 음수, null, 양수이면 반환

Math.pow(x,y)

x의 값을 y의 제곱근으로 반환

Math.sqrt(x)

x의 제곱근을 반환

Math.abs(x)

x의 절대값을 반환

Math.sin(x)

사인값 반환

Math.min()

작은값찾기

Math.random()

0~1사이 난수를 반환

Math.log()

로그 수를 반환

 

Math.random()

0포함 1제외 사이의 난수를 반환

항상 1보다 작은 숫자를 반환

Math.floor()도 임의의 정수를 반환 가능

Math.floor(Math.random() * 10);

적절한 랜덤함수의 예 :  Math.floor(Math.random() * (max - min) ) + min;

 

variablename = (condition) ? value1:value2 

728x90
반응형
BIG

댓글