Math 객체는 수학에서 자주 사용하는 상수와 함수들을 미리 구현해 놓은 JS 표준 내장 객체이다.
Math 객체는 생성자(Constructor)가 존재하지 않는다. 따라사 인스턴스 변수를 생성하지 않아도 Math 객체의 메서드를 바로 사용할 수 있다.
JS Math Method
메소드 | 설명 |
Math.min(x, y, ...) | 인수로 전달받은 값 중에서 가장 작은 수를 반환. |
Math.max(x, y, ...) | 인수로 전달받은 값 중에서 가장 큰 수를 반환. |
Math.random() | 0보다 크거나 같고 1보다 작은 랜덤 숫자(random number)를 실수로 반환. |
Math.round(x) | 입력값을 반올림한 수와 가장 가까운 정수 값을 반환. |
Math.floor(x) | 입력값을 내림하는 메서드, 소숫점 자리를 잘라버리고 가장 가까운 작은 정수 값을 반환 |
Math.ceil(x) | 입력값을 올림하는 메서드, 소숫점 자리를 버리고 가장 가까운 큰 정수 값을 반환 |
Math.abs(x) | 인수로 전달받은 값을 절대값으로 반환. |
Math.sqrt(x) | 인수로 전달받은 값을 루트값으로 반환. |
Math.pow(x,y) | 인수로 전달받은 값 x,y를 x^y 값으로 거듭제곱 값을 반환. |
Math.cbrt(x) | 인수로 전달받은 값의 세제곱근을 구한다. |
- Math.min(x, y...)
const num = Math.min(3,1,5,8,);
console.log(num); //1
Parameter 값 중 제일 작은 값을 Return 한다.
- Math.max(x, y,...)
const num = Math.max(3,1,5,8);
console.log(num);
Parameter 값 중 제일 큰 값을 Return 한다.
- Math.random()
const randomNum = Math.random(); //0~1 사이의 실수 값
console.log(randomNum);
/*만약 0~1사이 랜덤이 아닌 임의 숫자 사이의 난수를 발생시키고 싶다면*/
const userRandomNum = Math.random()*30 // ((0*30)~(1*30))사이
console.log(userRandomNum);

const hundredRandomNum = Math.round((Math.random()*30)+100); //100~130
console.log(hundredRandomNum);
Math.random() 메서드는 유사난수(pseudorandom) 넘버 0~1. 사이의 값을 Return 한다.
기본적으로 Math.random()은 0~1 사이값을 난수로 return 하는 메서드이다. 만약 100~130 사이의 난수값을 받고 싶다면,
(Math.random()*30) => 0~30 사이의 난수
(Math.random()*30)+100 = > (0+100)~(30+100) = 100~130 사이의 난수이다.
- Math.round()
let a = 1.2,
b = 1.9
c = -0.5;
a = Math.round(a);
b = Math.round(b);
c = Math.round(c);
console.log(`${a},${b},${c}`); //1,2,0
Math.round 메서드는 인수로 전달받은 값을 반올림하는 메서드이다.
- Math.floor()
let a = 1.2,
b = 1.9
c = -0.5;
a = Math.floor(a);
b = Math.floor(b);
c = Math.floor(c);
console.log(`${a},${b},${c}`); //1,1,-1
Math.floor 메서드는 인수로 전달받은 값을 무조건 내림하는 메서드이다.
- Math.ceil()
let a = 1.2,
b = 1.9
c = -0.5;
a = Math.ceil(a);
b = Math.ceil(b);
c = Math.ceil(c);
console.log(`${a},${b},${c}`); //2,2,0
Math.ceil() 메서드는 인수로 전달받은 값을 무조건 올림 하는 메서드이다.
- Math.abs()
let a = 5,
b = -5,
c = 5.5;
d = -5.5
console.log(Math.abs(a));
console.log(Math.abs(b));
console.log(Math.abs(c));
console.log(Math.abs(d));

Math.abs() 메서드는 parameter값을 절댓값으로 변환 후 return 하는 메서드이다.
- Math.sqrt()
let a = 16,
b = 64,
c = 2,
d = -1
console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
console.log(Math.sqrt(c));
console.log(Math.sqrt(d));
/* 4
** 8
** 1.41421356....
** NaN
*/
Math.sqrt()는 square root of a number를 리턴한다. 루트값을 리턴한다.
즉, Math.sqrt() 메서드로 제곱근값을 구할 수 있다.
- Math.pow(x, y)
console.log(Math.pow(2,3));
console.log(Math.pow(-2,3));
console.log(Math.pow(1.5,2));
/*
** 8
** -8
** 2.25
*/
Math.pow(x, y) 메서드는 parameter x 값이 밑 parameter y 값이 지수이다. 즉 x^y 값을 Return 하는 메서드이다.
Math.pow(x, y)로 거듭제곱을 구할 수 있다.
pow(x: number, y: number): number;
- Math.cbrt()
console.log(Math.cbrt(64)); //4^3 = 64 (4출력)
Math.cbrt() 메서드는 parameter 값의 세제곱근 값을 return해주는 메서드이다.
24.07.14
기본적으로 Math Object는 더 다양한 메서드와 프로퍼티가 있다. 대표적인 프로퍼티는 Math.PI
let piValue = Math.PI;
console.log(Math.floor(piValue*100)/100);
//3.14출력
등 다양하지만 추 후 정리가 필요할 때 계속적으로 업로드할 예정이다.
참고사이트(문서)
https://www.tcpschool.com/javascript/js_standard_math
코딩교육 티씨피스쿨
4차산업혁명, 코딩교육, 소프트웨어교육, 코딩기초, SW코딩, 기초코딩부터 자바 파이썬 등
tcpschool.com
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math
Math - JavaScript | MDN
Math 는 수학적인 상수와 함수를 위한 속성과 메서드를 가진 내장 객체입니다. 함수 객체가 아닙니다.
developer.mozilla.org
썸내일 출처
https://www.flaticon.com/kr/free-icon/js_5968292
'Javascript' 카테고리의 다른 글
[JS] const,let (0) | 2024.08.08 |
---|---|
[JS] 반응형 헤더, 햄버거 메뉴 (0) | 2024.07.13 |
[JS] D-day 실시간 계산 (0) | 2024.07.07 |