GeezerVison is showing developer.mozilla.org — Array.isArray() - JavaScript | MDN
Array.isArray()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
Array.isArray() 정적 메서드는 전달된 값이 Array인지 판단합니다.
console.log(Array.isArray([1, 3, 5]));
// Expected output: true
console.log(Array.isArray("[]"));
// Expected output: false
console.log(Array.isArray(new Array(5)));
// Expected output: true
console.log(Array.isArray(new Int16Array([15, 33])));
// Expected output: false
value가 Array 이면 true, 그렇지 않으면 false를 반환합니다. value가 TypedArray 인스턴스라면 항상 false가 반환됩니다.
js
// 아래 모든 호출은 true를 반환합니다.
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
Array.isArray(new Array("a", "b", "c", "d"));
Array.isArray(new Array(3));
// 거의 알려지지 않은 사실: Array.prototype 자체도 배열입니다.
Array.isArray(Array.prototype);
// 아래 모든 호출은 false를 반환합니다.
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray("Array");
Array.isArray(true);
Array.isArray(false);
Array.isArray(new Uint8Array(32));
// 배열 리터럴 구문이나 Array 생성자로 만들어지지 않았기 때문에 배열이 아닙니다.
Array.isArray({ __proto__: Array.prototype });
Array 인스턴스를 확인할 때 Array.isArray()는 교차 영역에서 작동하기 때문에 instanceof보다 선호됩니다.
js
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const xArray = window.frames[window.frames.length - 1].Array;
const arr = new xArray(1, 2, 3); // [1, 2, 3]
// 올바른 Array 확인 방법
Array.isArray(arr); // true
// arr의 프로토타입은 Array.prototype와 다른 객체인 xArray.prototype입니다.
arr instanceof Array; // false