β‘ JavaScript: Fix value instanceof Array Returning false for an Array Passed Across an iframe Boundary Because Each Window Has Its Own Array Constructor
Every JavaScript realm β the main page, and separately each iframe, popup window, or worker β has its own global object and therefore its own distinct set of built-in constructors, Array included. An array created inside an iframe and handed up to the parent page is a perfectly real array, but value instanceof Array fails in the parent’s realm, because that array’s prototype chain points to the iframe’s Array.prototype, not the parent window’s.
π The Problem
// inside the iframe: window.parent.receivedArray = [1, 2, 3]; // in the parent page: console.log(window.receivedArray instanceof Array); // false console.log(Array.isArray(window.receivedArray)); // true
β Fix: Check Array-ness Without Checking Whose Array It Is
- Use
Array.isArray(value)instead ofinstanceof Arrayβ it was specifically designed to correctly identify arrays regardless of which realm (iframe, window, worker) created them, by checking the object’s internal array exotic-object slot rather than walking one particular realm’s prototype chain. - If you genuinely need
instanceoffor a custom type (not Array itself) across a realm boundary, compare against the constructor the OTHER realm actually used β for exampleiframe.contentWindow.SomeClassβ rather than the one from your own scope. - When architecting cross-frame communication, prefer passing data through
structuredCloneor JSON serialization at the boundary whenever exact class identity doesn’t need to be preserved, since that sidesteps realm-identity mismatches for Arrays, Dates, and other built-ins entirely.
π¨ Why This Is Dangerous
This failure mode is especially confusing because the value genuinely IS an array in every observable way β Array.isArray on it returns true, bracket indexing works, .length is correct β so a check built only around instanceof can send debugging effort toward βwhy is this not an arrayβ when the real answer is βit’s an array from a different realm than the one you’re comparing against.β
instanceof asks ‘does this object’s prototype chain include YOUR Array,’ and across an iframe boundary the honest answer is no β it includes a different window’s Array that happens to work exactly the same way.

