Math객체

모찌모찌·2023년 11월 21일

자바스크립트 공부

목록 보기
17/27

• 절대값(Absolute Number)

: 어떤 값의 '양수(positive number)' 버전

console.log(Math.abs(-10)); //10
console.log(Math.abs(10)); //10

• 최대값(Maximum)

: Math.max 함수에 파라미터로 여러 수를 넘겨주면, 그중 가장 큰 값이 리턴됩니다.

console.log(Math.max(2,-2,1,5,0)); // 5

• 최솟값(Minimum)

Math.min()

• 거듭제곱(Exponentiation)

: Math.pow(x, y)를 하면 x의 y승의 결괏값이 리턴

console.log(Math.pow(2, 3)); //8
console.log(Math.pow(5, 2)); //25

• 제곱근(Square Root)

: '제곱'의 반대 ( 5의 제곱이 25이기 때문에, 25의 제곱근은 5)

console.log(Math.sqrt(25)); //5
console.log(Math.sqrt(49)); //7

• 반올림(Round)

: Math.round(x)

console.log(Math.round(2.4)); //2
console.log(Math.round(2.49)); //2
console.log(Math.round(2.5)); //3

• 버림과 올림 (Floor and Ceil)

-Math.floor(x) : x의 버림 값
-Math.ceil(x) : x의 올림 값

console.log(Math.floor(2.4));
console.log(Math.floor(2.49));
console.log(Math.floor(2.8)); //3줄 모두 결과값 2
console.log('-');
console.log(Math.ceil(2.4));
console.log(Math.ceil(2.49));
console.log(Math.ceil(2.8)); //3줄모두 결과값 3

• 난수(Random)

:Math.random을 하면 0 이상 1 미만의 값이 랜덤으로 리턴.

console.log(Math.random()); //0.6622040803059857
profile
꼬꼬마 개발자 지망생

0개의 댓글