e01.org

WebAssembly.JSTag

The JSTag read-only static property of the WebAssembly interface is a built-in WebAssembly.Tag representing exceptions thrown in the JavaScript host — it allows exceptions thrown in JavaScript to be handled from inside a Wasm module.

The **`JSTag`** read-only static property of the [`WebAssembly`](/docs/WebAssembly/Reference/JavaScript_interface) interface is a built-in [`WebAssembly.Tag`](/docs/WebAssembly/Reference/JavaScript_interface/Tag) representing exceptions thrown in the JavaScript host — it allows exceptions thrown in JavaScript to be handled from inside a Wasm module.

tabbed-taller

```wat interactive-example
(module
(tag $js_tag (import "env" "js_tag") (param externref))
(import "env" "do_work" (func $do_work))
(import "env" "log" (func $log (param externref)))

(func $try_and_catch
(block $handler (result externref)
(try_table (catch $js_tag $handler)
;; Call a JS function that throws
(call $do_work)
)
(return)
)
;; The JS Error object is on the stack as an externref
;; Pass it back to JS for logging
(call $log)
)

(export "try_and_catch" (func $try_and_catch))
)
```

```js interactive-example
async function run() {
const { instance } = await WebAssembly.instantiateStreaming(
fetch("{%wasm-url%}"),
{
env: {
js_tag: WebAssembly.JSTag,
// This JS function throws, which Wasm will catch via JSTag
do_work() {
throw new Error("An exception was thrown in JS");
},
log(error) {
// errRef is the JS Error object passed back as an externref
console.log(error.message);
},
},
},
);

instance.exports.try_and_catch();
}

run();
```

## Value

A [`WebAssembly.Tag`](/docs/WebAssembly/Reference/JavaScript_interface/Tag) with a type of [`externref`](/docs/WebAssembly/Reference/Value_types/externref) (`(tag (param externref))`).

## Specifications

## Browser compatibility

## See also

- [WebAssembly](/docs/WebAssembly) overview
- [`WebAssembly.Tag`](/docs/WebAssembly/Reference/JavaScript_interface/Tag)
- [tag](/docs/WebAssembly/Reference/Definitions/tag) Wasm definition

source: MDN Web Docs mirror · page

Cite this

"WebAssembly.JSTag". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/JSTag_static

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·