Barbarian Meets Codingbarbarianmeetscoding

WebDev, UX & a Pinch of Fantasy

2 minutes readlua

Lua

What is Lua?

Installing Lua

brew install lua luarocks

Once installed you can run the lua repl by writing lua in your terminal:

▶ lua
Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
>

Or interpret a lua script running the lua interpreter:

lua myprogram.lua

Variables

You can create variables in Lua by assignment a value to a variable like so:

isGood = true

number = 42
number2 = 12.23

str = 'a string'
str2 = "another string"
str3 = [[ a multiline string
  that expands
  multiple lines. ]]

Variables are global by default, you can create a local variable using the local keyword`:

number = 42        -- global variable
local number = 42  -- local variable (block scope)

Comments

-- This is a comment in lua
--[[ 
   This is a multiline comment in lua
--]]

Control Flow

if age > 21 then
  print('Allowed to drink beer! Yippie!')
elseif age > 20 then
  print('Almost there')
else
  print('Not allowed to drink beer')
end
while countdown > 0 do
  countdown = countdown - 1
end

Logic operators

== -- equal
~= -- unequal

Input and Output

print("hello world");
io.write("hello world")  -- Defaults to stdout

Resources


Jaime González García

Written by Jaime González García , Dad, Husband, Front-end software engineer, UX designer, amateur pixel artist, tinkerer and master of the arcane arts. You should follow him on Twitter where he shares useful stuff! (and is funny too).Jaime González García