Lua for JavaScript Developers
Transfer JavaScript experience to Lua 5.1: truth rules, tables, functions, module exports, errors and metatable objects. Build a stock ledger and port two source-language reference programs.
JavaScript and Lua side by side
The same program in JavaScript and in Lua, from the lessons of this course.
Print and Comment
JavaScript concatenates strings with +; Lua uses .. and print separates multiple arguments with tabs. Build one string for exact spacing.
JavaScript1console.log("Hi" + ", Ada");Lua1print("Hi" .. ", Ada")Undefined and Missing Fields
An object can retain a JavaScript property whose value is undefined. A Lua table field assigned nil is removed. False remains a stored Lua value.
JavaScript1const record = {x: undefined};2console.log("x" in record);Lua1local d = {x = nil}2print(d.x == nil)Ranges to Numeric for
JavaScript for has an explicit condition and update. Lua numeric for includes its limit, so the corresponding range is for i=1,3 do. A negative step is needed for descending Lua loops.
JavaScript1let total = 0;2for (let i = 1; i < 4; i++) total += i;3console.log(total);Lua1local total = 02for i = 1, 3 do total = total + i end3print(total)Functions and Returns
JavaScript and Lua functions are values. Missing JavaScript arguments become undefined; missing Lua arguments become nil. Lua uses function/end and no arrow syntax.
JavaScript1function twice(x) { return x * 2; }2console.log(twice(4));Lua1local function twice(x) return x * 2 end2print(twice(4))Lists as Dense Tables
JavaScript splice removes and shifts elements. Lua table.remove does the same with one-based indexes. JavaScript delete and Lua assignment to nil both risk holes instead of a dense sequence.
JavaScript1const a=["a","b","c"];2a.splice(1,1);3console.log(a.join(","));Lua1local a = {"a", "b", "c"}2table.remove(a, 2)3print(table.concat(a, ","))Sorting and Comparators
JavaScript sort mutates and returns the array; its comparator returns a signed number. Lua table.sort mutates but returns no result, and its comparator returns a strict boolean ordering.
JavaScript1const a=[3,1,2];2a.sort((x,y)=>x-y);3console.log(a[0]);Lua1local a={3,1,2}2table.sort(a)3print(a[1])
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFirst Lua Programs
6 lessonsValues and Decisions
5 lessonsIteration and Flow
5 lessonsFunctions and Closures
5 lessonsTables as Collections
5 lessonsOrdering, Modules and Errors
5 lessonsObjects and Owned State
5 lessonsComposition and Object Lookup
6 lessonsProject: Stock Ledger
4 lessonsFinal Source Ports
2 lessonsFrequently asked questions
Who is Lua for JavaScript Developers for?
For people who already write JavaScript and want to learn Lua. Each lesson starts from JavaScript you know and shows how Lua does the same thing, so you skip the beginner basics.
Do I need to know Lua already?
No. The course starts with your first Lua program. You need JavaScript basics: variables, conditions, loops, functions and collections.
How many lessons does Lua for JavaScript Developers have?
Lua for JavaScript Developers contains 48 interactive lessons you can complete at your own pace.
What does Lua for JavaScript Developers cover?
The course is organized into chapters covering: First Lua Programs, Values and Decisions, Iteration and Flow, Functions and Closures, and more.
Is Lua for JavaScript Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.