
Number.isInteger() 함수는 NaN을 정수로 간주하지 않습니다. 따라서 NaN을 입력했을 때 false를 반환합니다.
예시 코드는 다음과 같습니다.
#hostingforum.kr
javascript
console.log(Number.isInteger(1)); // true
console.log(Number.isInteger(NaN)); // false
NaN을 정수 여부로 처리할 때는 isNaN() 함수를 사용하는 것이 일반적입니다. 하지만 isNaN() 함수는 0을 NaN으로 간주하지 않기 때문에 주의가 필요합니다.
#hostingforum.kr
javascript
console.log(isNaN(NaN)); // true
console.log(isNaN(0)); // false
따라서 NaN을 정수 여부로 처리할 때는 Number.isInteger() 함수 대신에 isNaN() 함수를 사용하는 것이 좋습니다. 하지만 isNaN() 함수를 사용할 때는 0을 NaN으로 간주하지 않기 때문에 주의가 필요합니다.
#hostingforum.kr
javascript
function isInteger(value) {
return Number.isInteger(value) && !isNaN(value);
}
console.log(isInteger(1)); // true
console.log(isInteger(NaN)); // false
console.log(isInteger(0)); // false
2025-03-09 10:05