Learn COFFEESCRIPT with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
CoffeeScript Counter and Theme Toggle
count = 0
isDark = false
updateUI = ->
console.log "Counter: #{count}"
console.log "Theme: #{if isDark then 'Dark' else 'Light'}"
increment = ->
count += 1
updateUI()
decrement = ->
count -= 1
updateUI()
reset = ->
count = 0
updateUI()
toggleTheme = ->
isDark = !isDark
updateUI()
# Simulate actions
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()
Demonstrates a simple counter with theme toggling using CoffeeScript variables and console output.
2
CoffeeScript Simple Addition
a = 10
b = 20
sum = a + b
console.log "Sum: #{sum}"
Adds two numbers and prints the result.
3
CoffeeScript Factorial
factorial = (n) -> if n <= 1 then 1 else n * factorial(n - 1)
console.log "Factorial 5: #{factorial 5}"
Calculates factorial recursively.
4
CoffeeScript Fibonacci Sequence
fib = (n) -> if n < 2 then n else fib(n-1) + fib(n-2)
for i in [0..9]
console.log fib(i)
Generates first 10 Fibonacci numbers.
5
CoffeeScript Max of Two Numbers
a = 10
b = 20
max = if a > b then a else b
console.log "Max: #{max}"
Finds the maximum of two numbers.
6
CoffeeScript Array Sum
arr = [1,2,3,4,5]
sum = 0
for x in arr
sum += x
console.log "Sum: #{sum}"
Sums elements of an array.
7
CoffeeScript Even Numbers Filter
arr = [1,2,3,4,5]
for x in arr
console.log x if x % 2 is 0
Prints even numbers from an array.
8
CoffeeScript Conditional Counter Increment
count = 3
count += 1 if count < 5
console.log "Count: #{count}"
Increment counter only if less than 5.
9
CoffeeScript Resettable Counter
count = 0
count += 1
count += 1
console.log "Count: #{count}"
count = 0
console.log "Count: #{count}"
Counter that increments and can be reset.
10
CoffeeScript Theme Toggle Only
isDark = false
console.log "Theme: #{if isDark then 'Dark' else 'Light'}"
isDark = !isDark
console.log "Theme: #{if isDark then 'Dark' else 'Light'}"
isDark = !isDark
console.log "Theme: #{if isDark then 'Dark' else 'Light'}"
Toggles theme multiple times.