JS_Math객체

Mary·2025년 1월 24일
0

JavaScript

목록 보기
16/23
post-thumbnail

📌 JavaScript Math 객체 개념 정리 🚀

Math 객체는 JavaScript에서 수학적 연산을 수행할 수 있도록 제공하는 내장 객체야.
💡 특징:
Math객체(Object)이지만, 생성자 함수가 아님 (즉, new Math()로 인스턴스를 만들 수 없음).
Math 객체의 모든 메서드는 정적(static) 메서드이므로, Math.method() 형태로 사용.


Math 객체 주요 메서드 및 속성

📌 1. 기본 상수 (Math.PI, Math.E 등)

속성설명
Math.PI3.141592653589793원주율 (π)
Math.E2.718281828459045자연로그의 밑 (e)
Math.SQRT21.41421356237309512의 제곱근
Math.LN20.6931471805599453log(2)
Math.LN102.302585092994046log(10)

📌 사용 예시

console.log(Math.PI); // 3.141592653589793
console.log(Math.E);  // 2.718281828459045

📌 2. 반올림 & 정수 변환 (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

📌 3. 최대 & 최소 값 (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

📌 4. 랜덤 값 생성 (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);

📌 5. 거듭제곱 & 제곱근 (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)

📌 6. 로그 (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

📌 7. 삼각 함수 (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 객체는 다양한 수학적 연산을 도와주는 강력한 도구이며, 실무에서 자주 사용됨! 🚀

0개의 댓글