JavaScript Error.isError() Is Coming to V8: What the TC39 Proposal Means for Cross-Realm Error Checking
This article was written with the assistance of AI, under human supervision and review.
The Error.isError() static method checks whether a value is an Error instance using a branded check mechanism that works across realms, addressing silent failures in instanceof Error when errors cross iframe, worker, or Node.js vm module boundaries. The TC39 proposal reached Stage 4 and ships in Chrome 134+, Firefox 138+, and Node.js 23.0+.
Problem diagram showing instanceof Error failing with cross-realm error
Developers face two distinct failure modes with instanceof Error. An error thrown in an iframe and caught in the parent window returns false because each realm has its own Error constructor. The opposite problem hits when a plain object with Error.prototype in its chain passes the instanceof test despite lacking the internal error state. The Error.isError() method from MDN's official documentation uses the same branded check strategy as Array.isArray(), inspecting a private field initialized by the Error() constructor rather than walking the prototype chain.
Solution diagram showing Error.isError() correctly identifying cross-realm error
Key Takeaways
-
Error.isError()performs a branded check for a private field, avoiding both false positives from prototype manipulation and false negatives from cross-realm errors. - The method ships in Chrome 134+, Firefox 138+, and Node.js 23.0+, with no polyfill needed for environments where the check matters most.
- Global error handlers, test frameworks, and SSR runtimes benefit immediately because they aggregate errors from multiple execution contexts.
-
DOMExceptioninstances return true fromError.isError()despite not inheriting fromError, matching their branded behavior across the platform. - Migration from
instanceof Errorapplies only to code paths that receive errors from iframes, workers, or vm modules; local error handling remains unchanged.
Why instanceof Error Silently Fails in Modern JavaScript
The instanceof Error operator checks whether Error.prototype appears in the value's prototype chain. That check assumes the Error constructor in the current realm is the same constructor that created the error. When an error crosses a realm boundary, the assumption breaks. An error created in an iframe has iframeError.prototype as its prototype, not the parent window's Error.prototype. The instanceof check in the parent window returns false even though the value is a genuine error with a stack trace and message.
The second failure mode is the inverse problem. Developers can create a plain object, set its prototype to Error.prototype, and pass the instanceof Error check without the object ever being constructed by the Error() constructor. That object lacks the internal slots required for error handling, so operations like stack trace formatting fail. The pattern appears in test mocks and legacy codebases that manually wire up prototypes.
// False negative: real error from iframe const iframe = document.createElement('iframe'); document.body.appendChild(iframe); const iframeError = new iframe.contentWindow.Error('Cross-realm error'); console.log(iframeError instanceof Error); // false console.log(Error.isError(iframeError)); // true // False positive: prototype manipulation const fakeError = Object.create(Error.prototype); console.log(fakeError instanceof Error); // true console.log(Error.isError(fakeError)); // false The MDN execution model documentation defines realms as the boundaries where these checks fail. Each realm owns a list of intrinsic objects including Array, Error, and their prototypes. When code in one realm receives a value from another, the prototype identity check in instanceof compares against the wrong intrinsic.
JavaScript realms showing separate Error constructors in iframe and parent window
The TC39 proposal addresses both failure modes by checking for a private field that only the Error() constructor can initialize. That field exists regardless of realm, so an error from an iframe has the field even though its prototype chain points to a different Error.prototype. The branded check rejects fake errors because they lack the field, even if their prototype chain includes Error.prototype.
Understanding Realms: iframes, Workers, and vm Modules
A realm consists of a global object, a set of intrinsic objects, and the environment records for globally declared variables. Each iframe element creates a new realm. Web Workers and Service Workers execute in separate realms. Node.js provides the vm module to create isolated realms for running untrusted code. The MDN realm documentation specifies that each realm has its own globalThis and its own copies of built-in constructors.
Diagram showing realm hierarchy with separate Error constructors
The practical consequence is that iframe.contentWindow.Error !== window.Error. When an iframe throws an error and the parent catches it via postMessage or by calling a function that throws, the error's constructor is the iframe's Error, not the parent's. The instanceof operator in the parent compares against window.Error.prototype, finds no match, and returns false.
// Node.js vm module creates a separate realm const vm = require('vm'); const sandbox = { result: null }; const context = vm.createContext(sandbox); vm.runInContext(` try { throw new Error('Sandbox error'); } catch (e) { result = e; } `, context); console.log(sandbox.result instanceof Error); // false console.log(Error.isError(sandbox.result)); // true Workers communicate through postMessage, which uses structured cloning. Structured cloning serializes errors by preserving their message, stack, and cause, then reconstructs them in the receiving realm. The reconstructed error has the receiving realm's Error.prototype, so instanceof succeeds in the receiver but fails if the original error object is passed through a shared reference mechanism like SharedArrayBuffer metadata.
The vm module in Node.js exposes the realm boundary explicitly. Code executed in a vm context runs in a separate realm with its own global object and intrinsics. Errors thrown in that context belong to the context's Error constructor, not the main realm's. The Error.isError() method works in both directions: it identifies errors from vm contexts when called in the main realm, and it identifies main-realm errors when called inside a vm context.
How Error.isError() Works Under the Hood
The Error.isError() method performs a branded check by looking for a private field that the Error() constructor adds to every error instance. That private field is not accessible to JavaScript code and cannot be forged. The MDN Error.isError documentation states that the mechanism is the same as Array.isArray(), which checks for an internal slot rather than walking the prototype chain.
Flowchart showing Error.isError() branded check process
The private field is initialized in the Error() constructor before any user code runs. Subclasses like TypeError and RangeError inherit the same initialization, so Error.isError(new TypeError()) returns true. The check ignores the prototype chain entirely. An object can have Error.prototype as its prototype but lack the private field if it was created with Object.create(Error.prototype) instead of new Error().
The method returns true for DOMException instances even though DOMException does not inherit from Error in the prototype chain. The MDN documentation explains that DOMException behaves like Error for all branded checking purposes, meaning the DOMException() constructor initializes the same private field. This keeps error-handling code consistent across platform errors and JavaScript errors.
// All Error subclasses return true console.log(Error.isError(new Error())); // true console.log(Error.isError(new TypeError())); // true console.log(Error.isError(new RangeError())); // true // DOMException returns true despite not inheriting from Error console.log(Error.isError(new DOMException())); // true // Fake error returns false const fake = { __proto__: Error.prototype, message: 'fake' }; console.log(Error.isError(fake)); // false The branded check runs in constant time regardless of the prototype chain length. The instanceof operator must walk the entire prototype chain, checking each step against the target constructor's prototype. For deeply nested class hierarchies, instanceof becomes a linear-time operation. The branded check in Error.isError() accesses a single internal slot and returns, making it faster than instanceof in most cases.
The method accepts any value as its argument and returns false for non-objects without throwing. Passing undefined, null, a number, or a string to Error.isError() returns false. The same inputs cause instanceof Error to throw a TypeError when the right operand is not callable, though instanceof accepts primitives on the left side and returns false.
Diagram showing Error.isError() internal mechanism with private field check
Comparing Error Detection Methods: instanceof vs Object.prototype.toString vs Error.isError()
Developers have used three patterns to check for errors: instanceof Error, Object.prototype.toString.call(value) === '[object Error]', and now Error.isError(). Each has different failure modes and performance characteristics. The table shows when each method succeeds or fails.
| Method | Real error (same realm) | Real error (cross-realm) | Fake error (prototype only) | DOMException | Performance |
|---|---|---|---|---|---|
instanceof Error | ✓ | ✗ | ✓ (false positive) | ✗ | O(n) chain walk |
Object.prototype.toString | ✓ | ✓ | ✗ with Symbol.toStringTag | ✓ | O(1) but spoofable |
Error.isError() | ✓ | ✓ | ✗ | ✓ | O(1) branded check |
Comparison flowchart showing three error detection methods
The instanceof Error check walks the prototype chain until it finds Error.prototype or reaches null. That walk is O(n) where n is the chain length. For most errors n is small, but custom error subclasses with deep inheritance trees pay the cost. The check fails for cross-realm errors because it looks for the wrong Error.prototype, and it succeeds for fake errors because it only checks the chain.
The Object.prototype.toString.call(value) pattern returns '[object Error]' for genuine errors. That check worked reliably before ES2015 introduced Symbol.toStringTag. Now any object can override its string tag by defining Symbol.toStringTag, making the check spoofable. A plain object with { [Symbol.toStringTag]: 'Error' } returns '[object Error]' from toString even though it is not an error.
// instanceof fails cross-realm const iframe = document.createElement('iframe'); document.body.appendChild(iframe); const crossRealmError = new iframe.contentWindow.Error('test'); console.log(crossRealmError instanceof Error); // false console.log(Error.isError(crossRealmError)); // true // toString is spoofable const spoofed = { [Symbol.toStringTag]: 'Error', message: 'fake' }; console.log(Object.prototype.toString.call(spoofed)); // '[object Error]' console.log(Error.isError(spoofed)); // false // Error.isError() rejects fake errors const fake = Object.create(Error.prototype); console.log(fake instanceof Error); // true (false positive) console.log(Error.isError(fake)); // false The Error.isError() method provides the strongest guarantee. It returns true only for values created by the Error() constructor or its subclasses, regardless of realm. The branded check cannot be spoofed because the private field is not accessible from JavaScript. The method runs in constant time and allocates no memory, making it the fastest option.
The choice depends on the deployment environment. Local error handling in a single realm can continue using instanceof Error because the cross-realm case does not arise. Code that aggregates errors from multiple sources must use Error.isError() to avoid silent failures. Test frameworks, logging libraries, and global error handlers fall into the second category.
Real-World Code Examples: Global Error Handlers and Cross-Realm Error Detection
Global error handlers aggregate errors from the entire application, including iframes, workers, and dynamically loaded modules. The handlers must distinguish errors from non-error exceptions because JavaScript allows throwing any value. The pattern before Error.isError() was to check instanceof Error and fall back to wrapping non-error values.
// Before Error.isError(): unreliable global error handler window.addEventListener('error', (event) => { const error = event.error; // instanceof fails for cross-realm errors if (error instanceof Error) { console.error('Caught error:', error.message, error.stack); } else { // This branch catches cross-realm errors incorrectly console.error('Caught non-error:', error); } }); // After Error.isError(): reliable global error handler window.addEventListener('error', (event) => { const error = event.error; if (Error.isError(error)) { console.error('Caught error:', error.message, error.stack); } else { console.error('Caught non-error:', error); } }); Test frameworks encounter the cross-realm case when running tests in iframes or workers. The framework's assertion library might reject errors from the wrong realm if it uses instanceof Error to validate exception types. Switching to Error.isError() fixes the false negative without changing the test API.
// Test framework error normalization function normalizeError(caught: unknown): Error { // Before: instanceof Error fails for cross-realm errors if (caught instanceof Error) { return caught; } return new Error(String(caught)); } // After: Error.isError() handles cross-realm correctly function normalizeError(caught: unknown): Error { if (Error.isError(caught)) { return caught; } return new Error(String(caught)); } // Usage in test runner try { await runTestInIframe(testFn); } catch (error) { const normalized = normalizeError(error); console.error(`Test failed: ${normalized.message}`); console.error(normalized.stack); } Server-side rendering frameworks that run user code in isolated contexts benefit from Error.isError() when aggregating errors across contexts. Node.js applications using the vm module to sandbox user code need the cross-realm check to log errors correctly. The pattern normalizes errors before passing them to logging infrastructure.
The related iterator helpers post and Float16Array introduction show similar patterns where new built-in methods solve long-standing cross-realm issues. The TC39 type annotations proposal adds static types without changing runtime behavior, complementing the runtime safety that Error.isError() provides.
Browser and Runtime Support: Chrome 134+, Firefox 138+, and Node.js
The Error.isError() method ships in Chrome 134 and later, Firefox 138 and later, and Node.js 23.0 and later. Safari has not shipped the feature as of late 2026. The MDN browser compatibility data tracks implementation status across engines.
Browser support timeline for Error.isError()
Developers can check for support with a feature test before calling the method. The test avoids runtime errors in older environments and provides a fallback path. The fallback should use a conservative check that accepts more values as errors rather than rejecting genuine errors.
// Feature detection const hasErrorIsError = typeof Error.isError === 'function'; function isError(value: unknown): value is Error { if (hasErrorIsError) { return Error.isError(value); } // Fallback: conservative check // Accepts cross-realm errors and some false positives return value instanceof Error || (value !== null && typeof value === 'object' && 'message' in value && 'stack' in value); } Polyfilling Error.isError() is not possible because the private field is not accessible from JavaScript. The branded check requires engine support. Libraries that need cross-realm error detection before engine support arrives must use heuristics that check for error-like properties. Those heuristics accept false positives to avoid false negatives.
Node.js 23.0 ships the method in both the main thread and worker threads. The vm module creates contexts where Error.isError() is available on the sandbox's Error constructor. Errors created in a vm context can be checked with Error.isError() from the main thread, and errors from the main thread can be checked inside the vm context.
The lack of Safari support means production code targeting iOS must include the feature test and fallback. Web applications can serve the feature-detected code path to Chrome and Firefox users while serving the conservative fallback to Safari users. The performance difference between the two paths is negligible for error handling, which is not a hot path.
Migration Strategy: When to Replace instanceof Error in Your Codebase
Replacing instanceof Error with Error.isError() is not a blanket recommendation. The migration applies only to code paths that receive errors from multiple realms. Local error handling within a single realm sees no benefit from the change. The decision tree starts with identifying which code paths aggregate errors.
Decision flowchart for migrating from instanceof to Error.isError()
Global error handlers must migrate because they receive errors from all origins including iframes and workers. Test frameworks that execute tests in isolated contexts must migrate to avoid false negatives when validating thrown errors. Logging libraries that normalize caught exceptions before sending them to backend services should migrate to handle cross-realm errors correctly.
// Identify migration targets by scanning for these patterns // 1. Global error handlers window.addEventListener('error', handler); window.addEventListener('unhandledrejection', handler); // 2. Test framework assertion helpers expect(() => { throw error }).toThrow(Error); assert.throws(fn, Error); // 3. Cross-realm communication iframe.contentWindow.someFunction(); worker.postMessage({ type: 'task' }); // 4. Node.js vm module usage vm.runInContext(code, context); Code that only catches errors from local try-catch blocks does not need migration. Exception handling in pure functions, class methods, and async functions remains unchanged. The instanceof Error check works correctly when the error originates in the same realm.
The migration steps are: add a feature detection check, replace instanceof Error with the feature-detected isError() helper, and add test cases for cross-realm errors. The test cases should create an iframe or worker, throw an error from it, and verify the detection succeeds.
// Migration example: before class Logger { log(caught: unknown) { if (caught instanceof Error) { this.logError(caught); } else { this.logValue(caught); } } } // Migration example: after class Logger { private isError(value: unknown): value is Error { if (typeof Error.isError === 'function') { return Error.isError(value); } return value instanceof Error; } log(caught: unknown) { if (this.isError(caught)) { this.logError(caught); } else { this.logValue(caught); } } } The migration is non-breaking because Error.isError() returns true for all values where instanceof Error returns true in the same realm, plus it fixes the false negatives. Code that relied on the false positives from prototype manipulation breaks, but that code was already brittle. The false positives occur when developers intentionally create fake error objects, which is a rare pattern outside of tests.
Frequently Asked Questions
Does Error.isError() work with custom error classes?
Yes. Custom error classes that extend Error receive the same private field during construction, so Error.isError() returns true for instances of custom error classes regardless of which realm created them.
Can I polyfill Error.isError() in older browsers?
No. The branded check requires access to a private internal field that JavaScript cannot access. The best alternative is a feature-detected fallback that uses insta...










