JavaScript에서 숫자가 표현 가능한 최대 범위(Number.MAX_VALUE, 약 1.79 × 10308)를 초과하면 Infinity(양의 무한대) 또는 -Infinity(음의 무한대)로 자동 변환됩니다. 다음은 이러한 양수 및 음수 무한대 값을 출력하는 예제 코드입니다.
예제 코드
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.num {
font-size: 18px;
font-weight: 500;
}
</style>
</head>
<body>
<h1>JavaScript Infinity property</h1>
<div class="num">1.797693134862315E+10308</div>
<div class="num">-1.797693134862315E+10308</div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to add and subtract the above floating numbers by 1 respectively
</h3>
<script>
let sampleEle = document.querySelector(".sample");
let num1 = document.querySelectorAll(".num")[0];
let num2 = document.querySelectorAll(".num")[1];
document.querySelector(".Btn").addEventListener("click", () => {
num1.innerHTML = +num1.innerHTML + 1;
num2.innerHTML = +num2.innerHTML - 1;
});
</script>
</body>
</html>출력 결과
위 코드를 실행하면 다음과 같은 화면이 표시됩니다.

'CLICK HERE' 버튼을 클릭하면 −

코드 설명
화면에는 처음에 1.797693134862315E+10308과 -1.797693134862315E+10308이라는 두 개의 부동소수점 숫자가 표시됩니다. 이 값들은 이미 JavaScript가 표현할 수 있는 최대 범위를 훨씬 초과한 수치입니다.
버튼을 클릭하면 첫 번째 숫자에 1을 더하고(+num1.innerHTML + 1), 두 번째 숫자에서 1을 뺍니다(+num2.innerHTML - 1). 연산 결과가 허용 범위를 벗어나기 때문에 각각 Infinity와 -Infinity가 출력되는 것을 확인할 수 있습니다.