Learn Cobalt

Start at the top and go in order. Each lesson is short, every example can be copied and run, and the output is shown so you can check yours.

Lesson 1

Set up

Download cobalt.exe from the Get Cobalt page and double-click it once. It installs the cobalt command.

Open a new terminal (search for Terminal or PowerShell in the Start menu) and check that it worked. You should see a version number.

cobalt --version

Make a folder for your code. Open Notepad (or any editor), and save an empty file in that folder called hello.cbt. Every Cobalt file ends in .cbt.

To run a file, type cobalt and the file name in the terminal, in the same folder. You can also double-click the file.

cobalt hello.cbt

Lesson 2

Your first program

Put this line in hello.cbt, save it, and run it. out shows something on the screen. Text goes in double quotes.

out "Hello, world!"

Shows

Hello, world!

out can show several things at once, separated by commas. A line that starts with ~ is a comment: a note for people that Cobalt ignores.

~ my first program
out "I am", 14, "years old"

Shows

I am 14 years old

Try it yourself

Make a program that shows your name on one line and your favourite food on the next.

Show an answer
out "Om"
out "Pizza"

Shows

Om
Pizza

Lesson 3

Values

A value is a named box that holds something. := makes a value, or changes it if it already exists.

name := "Kai"
age := 15
out name, age

Shows

Kai 15

Put any value inside { } in text and it gets filled in.

name := "Kai"
out "Hi {name}! Next year you will be {15 + 1}."

Shows

Hi Kai! Next year you will be 16.

+= adds to a value. There is also -=, *= and /=.

coins := 10
coins += 5
coins -= 3
out coins

Shows

12

Try it yourself

Make a value called score that starts at 0. Add 10 to it twice, then show "Score: 20" using { }.

Show an answer
score := 0
score += 10
score += 10
out "Score: {score}"

Shows

Score: 20

Lesson 4

Math

Cobalt does the usual math. * is times, / is divide, and ** is power.

out 7 + 3, 7 - 3, 7 * 3, 7 / 2, 2 ** 3

Shows

10 4 21 3.5 8

// divides and drops the remainder. % gives just the remainder, which is handy for checking if a number is even.

out 17 // 5, 17 % 5
out 10 % 2 == 0

Shows

3 2
true

Comparisons give true or false: == (equal), != (not equal), <, >, <= and >=. Combine them with and, or, not.

age := 15
out age >= 13 and age <= 19
out not (age > 18)

Shows

true
true

Lesson 5

Checks

? checks something and runs the code after => only if it is true.

temp := 32
? temp > 30 => out "Hot today"

Shows

Hot today

Add more cases with |. The first true one wins, and _ means "anything else".

temp := 20
? temp > 30 => out "hot"
| temp > 15 => out "nice"
| _ => out "cold"

Shows

nice

To run several lines, end the line with => and indent the lines under it. Indentation is how Cobalt groups lines into a block.

coins := 3
? coins > 0 =>
coins -= 1
out "Bought one. {coins} left."
| _ =>
out "No coins"

Shows

Bought one. 2 left.

Try it yourself

Make a value marks := 72. Show "Pass" if marks is 40 or more, otherwise "Try again".

Show an answer
marks := 72
? marks >= 40 => out "Pass"
| _ => out "Try again"

Shows

Pass

Lesson 6

Lists and loops

A list holds many values in order, inside [ ]. Positions start at 0, and -1 is the last item.

fruits := ["apple", "kiwi", "mango"]
out fruits[0], fruits[-1], len fruits

Shows

apple mango 3

push adds an item to the end. sort gives a sorted copy.

scores := [70, 95, 82]
scores.push 60
out scores.sort

Shows

[60, 70, 82, 95]

each goes through a list one item at a time.

["apple", "kiwi"] each fruit => out "I like {fruit}"

Shows

I like apple
I like kiwi

loop repeats code. Give it a number, or a check to repeat while it is true. 1..3 makes the list [1, 2, 3].

loop 2 => out "hi"
1..3 each n => out n * 10

Shows

hi
hi
10
20
30

Try it yourself

Show the 5 times table from 5 × 1 to 5 × 5, one line each, like "5 x 3 = 15".

Show an answer
1..5 each n => out "5 x {n} = {5 * n}"

Shows

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25

Lesson 7

Maps

A map stores values by name, inside { }. Read one with a dot.

pet := {name: "Milo", kind: "cat", age: 3}
out pet.name, "is a", pet.kind

Shows

Milo is a cat

Change or add a value the same way. keys lists the names.

pet := {name: "Milo", age: 3}
pet.age := 4
pet.color := "black"
out keys pet

Shows

["name", "age", "color"]

A list of maps is how most apps keep their data.

tasks := [{title: "Homework", done: true}, {title: "Practice", done: false}]
tasks each t => out t.title, t.done

Shows

Homework true
Practice false

Lesson 8

Actions

An action is a piece of code with a name that you can run again and again. Make one with fx. give hands back a result.

fx greet name =>
give "Hello, {name}!"
out greet "Mia"

Shows

Hello, Mia!

Short actions fit on one line. The last value is given back by itself.

fx square n => n * n
out square 6

Shows

36

For more than one input, use brackets when you call it.

fx add a, b => a + b
out add(2, 3)

Shows

5

Try it yourself

Make an action called double that gives back a number times 2, then show double 21.

Show an answer
fx double n => n * 2
out double 21

Shows

42

Lesson 9

Live values

Use <- instead of := and the value updates itself whenever what it uses changes, like a cell in a spreadsheet.

price := 20
tax := 2
total <- price + tax
out total
price := 50
out total

Shows

22
52

In apps this saves a lot of work: totals and counts stay correct without you updating them.

cart := [5, 12, 3]
count <- len cart
cart.push 8
out count

Shows

4

Lesson 10

Time travel

Every value remembers its past. history shows it, and rewind goes back a step.

score := 10
score := 25
score := 40
out score.history
rewind score
out score

Shows

[10, 25, 40]
25

This is how an Undo button works in Cobalt: one rewind.

balance := 100
balance -= 30
out balance
rewind balance
out "after undo:", balance

Shows

70
after undo: 100

Lesson 11

Pipes and match

>> passes a value to the next step, so you can read the steps in order.

[5, 2, 9]
>> sort
>> map fx x => x * 10
>> out

Shows

[20, 50, 90]

Put ? after a value to compare it against ranges and options. 90.. means 90 or more.

fx grade points =>
give points ? 90.. => "A"
| 75..89 => "B"
| _ => "C"
out grade 82

Shows

B

Lesson 12

Watchers

on watches a check and runs its code the moment it becomes true.

score := 0
on score >= 3 => out "Level up!"
loop 4 => score += 1
out score

Shows

Level up!
4

Lesson 13

Your first app

Add a window block and your program opens as its own app. Save this as counter.cbt and run it.

count := 0
 
window "Counter" =>
title "{count}"
button "Add one" => count += 1

Run it and a window opens.

Every time a value changes, Cobalt redraws the window, so it always shows the current values. You never update the screen yourself.

Buttons can have a kind and an icon. toast shows a small message.

window "Hello" =>
title "Hello!"
button "Wave", kind: "primary", icon: "heart" => toast "Hi there!"

Run it and a window opens.

Lesson 14

Inputs

bind: connects a control to a value. Type in the box and the value changes, and the title updates by itself.

name := ""
 
window "Greeter" =>
input "Your name", bind: name
title "Hello, {name}!"

Run it and a window opens.

Sliders, switches and checkboxes work the same way.

size := 40
bold := false
 
window "Text size" =>
slider min: 10, max: 80, bind: size
switch "Bold", bind: bold
text "Cobalt", size: size, bold: bold

Run it and a window opens.

Try it yourself

Make an app with a slider from 0 to 100 and a title that says "Volume: " and the slider's value.

Show an answer
volume := 50
 
window "Volume" =>
slider min: 0, max: 100, bind: volume
title "Volume: {volume}"

Lesson 15

Layout and style

row puts things side by side, column stacks them, and card draws a box around them. Blocks go inside each other with indentation.

window "Profile" =>
card =>
row =>
avatar "Kai"
column =>
title "Kai"
text "Likes chess and coding"

Run it and a window opens.

Any block takes style options like color, size, padding and radius. animate: makes it move.

window "Style" =>
card padding: 24, radius: 16 =>
title "Hello", color: "#60a5fa", animate: "fade-up"
text "Hover me", hover: {scale: 1.1}, transition: "0.2s"

Run it and a window opens.

Lesson 16

Saving data

load gets saved data, or a starting value the first time. save stores it, so it is still there next time you open the app.

visits := load "visits", 0
visits += 1
save "visits", visits
 
window "Visits" =>
title "You opened this {visits} times"

Run it and a window opens.

Here is everything together: a small to-do list that remembers your tasks.

tasks := load "tasks", []
draft := ""
 
fx add =>
? draft == "" => give
tasks.push {name: draft, done: false}
draft := ""
save "tasks", tasks
 
window "My Tasks" =>
row =>
input "New task", bind: draft, enter: (fx t => add())
button "Add" => add
tasks each i, task =>
checkbox task.name, bind: tasks[i].done => save "tasks", tasks

Run it and a window opens.

Lesson 17

What next

You now know enough to build real apps. Read the Ledger example to see a bigger app, or open the full guide on GitHub for every built-in action and window block.

If something goes wrong, Cobalt's error messages say what happened, show the line, and usually suggest a fix.