WebAssembly.Global() constructor
A WebAssembly.Global() constructor creates a new Global object representing a global variable instance, accessible from both JavaScript and importable/exportable across one or more WebAssembly.Module instances.
A **`WebAssembly.Global()`** constructor creates a new `Global` object representing a global variable instance, accessible from both JavaScript and importable/exportable across one or more [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) instances.
This allows dynamic linking of multiple modules.
## Syntax
```js-nolint
new WebAssembly.Global(descriptor, value)
```
### Parameters
- `descriptor`
- : An object containing two properties:
- `value`
- : A string representing the data type of the global. This can be any one of:
- `i32`: A 32-bit integer.
- `i64`: A 64-bit integer. (In JavaScript, this is represented as a BigInt)
- `f32`: A 32-bit floating point number.
- `f64`: A 64-bit floating point number.
- [`funcref`](/docs/WebAssembly/Reference/Value_types/funcref)
- [`externref`](/docs/WebAssembly/Reference/Value_types/externref)
- `mutable`
- : A boolean value that determines whether the global is mutable or not. By default, this is `false`.
- `value`
- : The value the variable contains. This can be any value, as long as its type matches the variable's data type.
If no value is specified:
- a typed `0` value is used if `descriptor.value` is `i32`, `i64`, `f32`, or `f64`
- a reference to `undefined` is used if `descriptor.value` is `externref`
- `null` is used if `descriptor.value` is `funcref`
### Exceptions
- TypeError
- : Thrown if any of these conditions is met:
- `descriptor` is not an object.
- `descriptor.value` is missing or not a valid value type (such as `"v128"`).
- `value` is specified but cannot be converted to the WebAssembly type named by `descriptor.value`.
## Examples
### Creating a new Global instance
The following example shows a new global instance being created using the `WebAssembly.Global()` constructor.
It is being defined as a mutable `i32` type, with a value of 0.
The value of the global is then changed, first to `42` using the `Global.value` property, and then to 43 using the `incGlobal()` function exported out of the `global.wasm` module (this adds 1 to whatever value is given to it and then returns the new value).
```js
const output = document.getElementById("output");
function assertEq(msg, got, expected) {
const result =
got === expected
? `SUCCESS! Got: ${got}\n`
: `FAIL!\nGot: ${got}\nExpected: ${expected}\n`;
output.innerText += `Testing ${msg}: ${result}`;
}
assertEq("WebAssembly.Global exists", typeof WebAssembly.Global, "function");
const global = new WebAssembly.Global({ value: "i32", mutable: true }, 0);
WebAssembly.instantiateStreaming(fetch("global.wasm"), { js: { global } }).then(
({ instance }) => {
assertEq(
"getting initial value from wasm",
instance.exports.getGlobal(),
0,
);
global.value = 42;
assertEq(
"getting JS-updated value from wasm",
instance.exports.getGlobal(),
42,
);
instance.exports.incGlobal();
assertEq("getting wasm-updated value from JS", global.value, 43);
},
);
```
> [!NOTE]
> You can see the example [running live on GitHub](https://mdn.github.io/webassembly-examples/js-api-examples/global.html);
> see also the [source code](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/global.html).
## Specifications
## Browser compatibility
## See also
- [`global`](/docs/WebAssembly/Reference/Definitions/global) definition
- [WebAssembly](/docs/WebAssembly) overview
- [WebAssembly concepts](/docs/WebAssembly/Guides/Concepts)
- [Using the WebAssembly JavaScript API](/docs/WebAssembly/Guides/Using_the_JavaScript_API)
Cite this
"WebAssembly.Global() constructor". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/Global/Global