
Array.isArray() 함수는 객체의 인스턴스인지 아닌지 확인하는 함수입니다. 이 함수는 Array.prototype을 상속받은 객체만 true를 반환합니다.
null과 undefined는 객체의 인스턴스가 아니기 때문에 false를 반환해야 하지만, Array.isArray() 함수의 경우 null과 undefined를 객체의 인스턴스로 간주합니다.
이러한 이유는 ECMAScript Specification에 따라, null과 undefined는 객체의 인스턴스라고 정의되어 있기 때문입니다.
따라서, Array.isArray() 함수를 사용하여 정확한 결과를 얻으려면, Array.prototype을 상속받은 객체만 넣어야 합니다. 예를 들어, [1, 2, 3] 또는 {a: 1, b: 2}와 같은 객체를 넣어야 합니다.
아래의 예제를 참고하세요.
#hostingforum.kr
javascript
console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray({a: 1, b: 2})); // false
console.log(Array.isArray(null)); // true
console.log(Array.isArray(undefined)); // true
console.log(Array.isArray(1)); // false
console.log(Array.isArray("hello")); // false
위의 예제에서, Array.isArray() 함수는 Array.prototype을 상속받은 객체인 [1, 2, 3]에 대해서만 true를 반환합니다.
2025-07-28 10:21