HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2020.
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. This allows dynamic linking of multiple modules.
WebAssembly.Global()
Global
WebAssembly.Module
new WebAssembly.Global(descriptor, value)
descriptor
An object, which contains two properties:
value
i32
i64
BigInt
f32
f64
v128
externref
anyfunc
mutable
false
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 where the value of descriptor.value is one of i32, i64, f32, or f64, and null is used if descriptor.value is externref or anyfunc (as specified by the DefaultValue algorithm).
descriptor.value
null
DefaultValue
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).
42
Global.value
incGlobal()
global.wasm
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; see also the source code.
Enable JavaScript to view this browser compatibility table.