본문 바로가기

정리 모음 ✨

[자바스크립트] math 메서드 정리 ✨

참고자료1

 

[JS] 📚 자바스크립트 Math 메소드 💯 총정리

자바스크립트 Math 메소드 Math 객체는 수학 상수와 함수를 위한 프로퍼티와 메소드를 제공하는 빌트인 객체이다. Math 객체는 생성자 함수가 아니다. 따라서 Math 객체는 정적(static) 프로퍼티와 메

inpa.tistory.com

 

 

✨ math method

 

💻 math.abs(number)

- 인수의 절댓값 반환

math.abs(-1); //1
math.abs('-1'); //1
math.abs(''); //0
math.abs([]); //0
math.abs(null); //0
math.abs(undefined) //0

 

 

 

💻math.round(number): number

- 인수의 소수점 이하를 반올림하는 정수를 반환

math.round(1.4); //1
math.round(1.6); //2
math.round(-1.4); //-1
math.round(); //NaN

 

 

💻 math.ceil(number)

- ceil은 `천장`이라는 뜻

- 인수의 소수점 이하를 올림하는 정수를 반환

Math.ceil(1.4);  // 2
Math.ceil(1.6);  // 2
Math.ceil(-1.4); // -1
Math.ceil(-1.6); // -1
Math.ceil(1);    // 1
Math.ceil();     // NaN

 

 

💻 math.floor(number)

- 인수의 소수점 이하를 내림한 정수 반환

- math.ceil의 반대 개념

Math.floor(1.9);  // 1
Math.floor(9.1);  // 9
Math.floor(-1.9); // -2
Math.floor(-9.1); // -10
Math.floor(1);    // 1
Math.floor();     // NaN

 

 

💻math.sqrt(number)

- sqrt: square root, 제곱의 루트

- 인수의 제곱근을 반환

math.sqrt(9); //3
math.sqrt(-9); //NaN
math.sqrt(2); //1.41421...
math.sqrt(1); //1

 

 

💻math.random();

- 0부터 1미만의 랜덤 부동 소수점

 

💻 math.pow();

- `pow`: power(거듭제곱)

- 거듭제곱 반환

math.pow(2, 8); //256
math.pow(2, -1); //0.5