Learn MOONSCRIPT with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
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.
2
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.
3
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.
4
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.
5
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.
6
MoonScript Dice Roller
rollDice = ->
result = math.random 1, 6
print "Rolled: #{result}"
# Simulate actions
rollDice!
rollDice!
rollDice!
Rolls a dice and prints the result.
7
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.
8
MoonScript Name Greeting
greet = (name) ->
print "Hello, #{name}! Welcome!"
# Simulate actions
greet 'Saurav'
greet 'Alice'
greet 'Bob'
Greets the user with their name.
9
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.
10
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.