Learn Moonscript - 10 Code Examples & CST Typing Practice Test
MoonScript is a high-level, dynamic programming language that compiles to Lua. It provides syntactic sugar, cleaner syntax, and modern language features like classes, comprehensions, and pattern matching, while maintaining full compatibility with Lua runtime.
View all 10 Moonscript code examples →
Learn MOONSCRIPT with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
MoonScript Counter and Theme Toggle
count = 0
isDark = false
updateUI = ->
print "Counter: #{count}"
print "Theme: #{if isDark then 'Dark' else 'Light'}"
increment = ->
count += 1
updateUI!
decrement = ->
count -= 1
updateUI!
reset = ->
count = 0
updateUI!
toggleTheme = ->
isDark = not isDark
updateUI!
# Simulate actions
updateUI!
increment!
increment!
toggleTheme!
decrement!
reset!
Demonstrates a simple counter with theme toggling using MoonScript variables and functions.
MoonScript Random Number Generator
generate = ->
num = math.random 1, 100
parity = if num % 2 == 0 then 'Even' else 'Odd'
print "Number: #{num}, #{parity}"
# Simulate actions
generate!
generate!
generate!
Generates a random number and shows if it is odd or even.
MoonScript Todo List
todos = {}
updateUI = ->
print "Todo List:"
for i, task in ipairs todos
print "#{i}. #{task}"
print "----------------"
addTask = (task) ->
table.insert todos, task
updateUI!
removeTask = (index) ->
table.remove todos, index
updateUI!
# Simulate actions
addTask 'Buy milk'
addTask 'Write MoonScript code'
removeTask 1
Maintains a simple todo list with add and remove functions.
MoonScript Stopwatch
time = 0
running = false
updateUI = ->
print "Stopwatch: #{time} seconds"
start = ->
running = true
print 'Stopwatch started'
stop = ->
running = false
print 'Stopwatch stopped'
reset = ->
time = 0
updateUI!
# Simulate actions
updateUI!
start!
time += 5
updateUI!
stop!
reset!
Simulates a stopwatch with start, stop, and reset functionality.
MoonScript Temperature Converter
celsiusToF = (c) -> (c * 9 / 5) + 32
fToCelsius = (f) -> (f - 32) * 5 / 9
updateUI = (temp, scale) ->
if scale == 'C'
print "#{temp}°C = #{celsiusToF temp}°F"
else
print "#{temp}°F = #{fToCelsius temp}°C"
# Simulate actions
updateUI 25, 'C'
updateUI 77, 'F'
Converts Celsius to Fahrenheit and vice versa.
MoonScript Dice Roller
rollDice = ->
result = math.random 1, 6
print "Rolled: #{result}"
# Simulate actions
rollDice!
rollDice!
rollDice!
Rolls a dice and prints the result.
MoonScript Countdown Timer
count = 5
updateUI = ->
print "Countdown: #{count}"
startCountdown = ->
while count > 0
updateUI!
count -= 1
updateUI!
print 'Done!'
# Simulate actions
startCountdown!
Counts down from a number to zero.
MoonScript Name Greeting
greet = (name) ->
print "Hello, #{name}! Welcome!"
# Simulate actions
greet 'Saurav'
greet 'Alice'
greet 'Bob'
Greets the user with their name.
MoonScript Prime Checker
isPrime = (n) ->
return false if n < 2
for i in [2..math.floor(math.sqrt n)]
return false if n % i == 0
true
checkNumber = (num) ->
print "#{num} is #{if isPrime num then 'Prime' else 'Not Prime'}"
# Simulate actions
checkNumber 7
checkNumber 10
checkNumber 13
Checks if a number is prime.
MoonScript Shopping Cart
cart = {}
total = 0
updateUI = ->
print "Cart Items:"
for item, price in pairs cart
print "#{item}: $#{price}"
print "Total: $#{total}"
print '----------------'
addItem = (item, price) ->
cart[item] = price
total += price
updateUI!
removeItem = (item) ->
if cart[item]
total -= cart[item]
cart[item] = nil
updateUI!
# Simulate actions
addItem 'Apple', 2
addItem 'Banana', 3
removeItem 'Apple'
Simulates adding and removing items in a shopping cart with total cost.
Frequently Asked Questions about Moonscript
What is Moonscript?
MoonScript is a high-level, dynamic programming language that compiles to Lua. It provides syntactic sugar, cleaner syntax, and modern language features like classes, comprehensions, and pattern matching, while maintaining full compatibility with Lua runtime.
What are the primary use cases for Moonscript?
Scripting for games (Love2D, Roblox). Writing concise Lua programs. Rapid prototyping in Lua environments. Embedding in applications with Lua runtime. Creating libraries compatible with Lua
What are the strengths of Moonscript?
Improves code readability over Lua. Maintains full Lua compatibility. Supports modern programming patterns. Lightweight with zero runtime overhead. Ideal for rapid prototyping
What are the limitations of Moonscript?
Dependent on Lua runtime. Smaller community than Lua itself. Debugging maps to generated Lua code. Limited IDE/editor support. Less suitable for large enterprise codebases without Lua experience
How can I practice Moonscript typing speed?
CodeSpeedTest offers 10+ real Moonscript code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.