Back to Content

local: Wasm text instruction

files/en-us/webassembly/reference/variables/local/index.md

latest650 B
Original Source

The local instruction declares a new local variable.

{{InteractiveExample("Wat Demo: local", "tabbed-taller")}}

wat
(module
  (import "console" "log" (func $log (param i32)))
  (func $main

    (local $var i32) ;; create a local variable named $var
    (local.set $var (i32.const 10)) ;; set $var to 10
    local.get $var ;; load $var onto the stack
    call $log ;; log the result

  )
  (start $main)
)
js
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });

Syntax

wat
;; declare new variable named $val of type i32
(local $val i32)