Learn the Basics of Lua as quickly as possible

Finn
2 min readDec 13, 2022

--

By the way, this is part of a huge collection of notes and flashcards called CodingNotes. Check it out here https://www.codingnotes.io/ .

Learn the Basics of a language in a matter of minutes ( Source )

Installation ( quick)

💡 Install the binary and add the path to Lua.

💡 In a terminal, type lua54 to get into lua , os.exit() to quit.

💡 In any editor, just cd into to folder and say lua54 file.lua to execute it.

Print something

print("Hello World")
print(5*3)
io.write("Hello World")
io.write(5*3)

Difference print, io.write

  • Unlike print , io.write adds no extra characters to the output, such as tabs or newlines.
  • io.write uses the current output file, whereas print always uses the standard output.
  • print automatically applies tostring to its arguments, so it can also show tables, functions, and nil.

You will mostly use print()

Comments

-- single line -- 
--[[ 
multine line
]]

Variables

💡 Variable names can not start with number but can contain letter, numbers and underscores.

💡 They are dynamically typed ( e.g. no str declaration for string )

name = "John"
age = 16

Strings

💡 Strings can be either in double or singlequotes

name = "Mister Smith"
name = 'Mister Josh'
longString = [[
I am a string
which is long
]]
io.write(longString, "\\n")

Concatenating strings

longString = [[
I am a string
which is long
]]
name = " What is going on "
longString = longString .. name
print(longString)

Numbers

💡 There are only floating point numbers , they are precise up to 13 digits.

Checking the datatype

num = 78349238749284
io.write(type(num))

💡 If no value is assigned , it gets the value/type of nil .

Booleans

isAbleToDrive = true
io.write(type(isAbleToDrive))

Reassigning value

💡 It is completely valid to reassign a variable even if this changing its datatyp. The program will just choose the lastest datatyp with its appropriate value.

name = "John"
io.write("Size of this string is:", #name , "\\n")
name = 30
io.write("Now my name is the number ", name , "\\n")

More

#number
  • lists the amount of characters a string or number has
io.write("Hello" "\\n")
  • ensures that there will be a new line after “Hello” ( print does that by default if “Hello” is the last word)

Thanks for reading !

This comprehensive explanation is part of a huge collection of notes and flashcards called CodingNotes. You can check it out here https://www.codingnotes.io/ .

--

--

Finn

Software developer 👨‍💻 and Notion enthusiast✍️ driven to make the world better. Visit me at https://finnguha.dev ✅ and https://twitter.com/coding_notes 🚀