typestar

local in Lua

local scopes a name to its block; bare assignment goes global.

-- local scopes a name to its block; bare assignment goes global.
local hero = "Wren"
local hp = 20

do
  -- A do block opens a fresh scope; this hp shadows the outer one.
  local hp = hp + 5
  print(hero .. " drinks a tonic: " .. hp .. " hp inside the vault")
end

print("Back outside, hp is still " .. hp)

-- Multiple assignment binds left to right.
local gold, gems = 120, 3
print(string.format("Purse: %d gold, %d gems", gold, gems))

How it works

  1. local hero lives to the end of its enclosing block.
  2. A do ... end block opens a fresh scope for shadowing.
  3. local gold, gems = 120, 3 assigns multiply, left to right.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
432
Tokens
63
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 54 seconds.

Type this snippet

Step 2 of 3 in Values, step 2 of 27 in Language basics.

← Previous Next →