
Math 객체 개념 정리 🚀Math 객체는 JavaScript에서 수학적 연산을 수행할 수 있도록 제공하는 내장 객체야.
💡 특징:
✅ Math는 객체(Object)이지만, 생성자 함수가 아님 (즉, new Math()로 인스턴스를 만들 수 없음).
✅ Math 객체의 모든 메서드는 정적(static) 메서드이므로, Math.method() 형태로 사용.
Math 객체 주요 메서드 및 속성Math.PI, Math.E 등)| 속성 | 값 | 설명 |
|---|---|---|
Math.PI | 3.141592653589793 | 원주율 (π) |
Math.E | 2.718281828459045 | 자연로그의 밑 (e) |
Math.SQRT2 | 1.4142135623730951 | 2의 제곱근 |
Math.LN2 | 0.6931471805599453 | log(2) |
Math.LN10 | 2.302585092994046 | log(10) |
📌 사용 예시
console.log(Math.PI); // 3.141592653589793
console.log(Math.E); // 2.718281828459045
round, floor, ceil, trunc)| 메서드 | 설명 | 예제 |
|---|---|---|
Math.round(x) | 반올림 (소수점 첫째 자리 기준) | Math.round(4.6) → 5 |
Math.floor(x) | 내림 (소수점 버림) | Math.floor(4.9) → 4 |
Math.ceil(x) | 올림 (소수점 올림) | Math.ceil(4.1) → 5 |
Math.trunc(x) | 소수점 이하 제거 | Math.trunc(4.9) → 4 |
📌 사용 예시
console.log(Math.round(4.5)); // 5
console.log(Math.floor(4.9)); // 4
console.log(Math.ceil(4.1)); // 5
console.log(Math.trunc(4.9)); // 4
max, min)| 메서드 | 설명 | 예제 |
|---|---|---|
Math.max(a, b, c, ...) | 주어진 값 중 최대값 반환 | Math.max(10, 5, 8) → 10 |
Math.min(a, b, c, ...) | 주어진 값 중 최소값 반환 | Math.min(10, 5, 8) → 5 |
📌 사용 예시
console.log(Math.max(1, 3, 7, 5)); // 7
console.log(Math.min(1, 3, 7, 5)); // 1
random)| 메서드 | 설명 | 예제 |
|---|---|---|
Math.random() | 0 이상 ~ 1 미만의 난수 생성 | Math.random() → 0.23456 |
랜덤 정수 생성 | 특정 범위의 랜덤 정수 | Math.floor(Math.random() * (최댓값 - 최솟값 + 1) + 최솟값) |
📌 사용 예시
console.log(Math.random()); // 0 이상 1 미만의 랜덤 값
// 1부터 100까지의 랜덤 정수
console.log(Math.floor(Math.random() * 100) + 1);
pow, sqrt)| 메서드 | 설명 | 예제 |
|---|---|---|
Math.pow(x, y) | x의 y제곱 반환 | Math.pow(2, 3) → 8 |
Math.sqrt(x) | x의 제곱근 반환 | Math.sqrt(9) → 3 |
📌 사용 예시
console.log(Math.pow(2, 3)); // 8 (2^3)
console.log(Math.sqrt(16)); // 4 (√16)
log, log10)| 메서드 | 설명 | 예제 |
|---|---|---|
Math.log(x) | 자연로그 (ln x) | Math.log(Math.E) → 1 |
Math.log10(x) | 밑이 10인 로그 (log₁₀ x) | Math.log10(1000) → 3 |
📌 사용 예시
console.log(Math.log(Math.E)); // 1
console.log(Math.log10(1000)); // 3
sin, cos, tan)| 메서드 | 설명 | 예제 |
|---|---|---|
Math.sin(x) | 사인 값 반환 (라디안 기준) | Math.sin(Math.PI / 2) → 1 |
Math.cos(x) | 코사인 값 반환 | Math.cos(0) → 1 |
Math.tan(x) | 탄젠트 값 반환 | Math.tan(Math.PI / 4) → 1 |
📌 사용 예시
console.log(Math.sin(Math.PI / 2)); // 1 (90도)
console.log(Math.cos(0)); // 1
console.log(Math.tan(Math.PI / 4)); // 1 (45도)
| 메서드 | 설명 | 예제 |
|---|---|---|
Math.round(x) | 반올림 | Math.round(4.5) → 5 |
Math.floor(x) | 내림 | Math.floor(4.9) → 4 |
Math.ceil(x) | 올림 | Math.ceil(4.1) → 5 |
Math.trunc(x) | 소수점 이하 제거 | Math.trunc(4.9) → 4 |
Math.max(a, b, c, ...) | 최댓값 | Math.max(10, 5, 8) → 10 |
Math.min(a, b, c, ...) | 최솟값 | Math.min(10, 5, 8) → 5 |
Math.random() | 0 이상 1 미만의 난수 | Math.random() → 0.23456 |
Math.pow(x, y) | 거듭제곱 | Math.pow(2, 3) → 8 |
Math.sqrt(x) | 제곱근 | Math.sqrt(9) → 3 |
Math.log(x) | 자연로그 | Math.log(Math.E) → 1 |
Math.sin(x) | 사인 값 | Math.sin(Math.PI / 2) → 1 |
👉 즉, Math 객체는 다양한 수학적 연산을 도와주는 강력한 도구이며, 실무에서 자주 사용됨! 🚀