Version: v0.7.0 - Beta.  We welcome contributors & feedback.

Math & Logic

Math Operators

You can modify numbers by using the math operators + - * /.

Expressions can be grouped together with parentheses ( ).

\$numPosts = 2 + 3

\$numKarma = (\$numPosts * 2) + numUpvotes
//= 20

Combined Operators

Math operators can be combined with the = operator, as an easy way to modify the current variable.

\$numKarma = 5
\$numKarma += 2  //= 7

// same as...
\$numKarma = \$numKarma + 2

// More examples
\$a += 2  // \$a = \$a + 2
\$a -= 2  // \$a = \$a - 2
\$a *= 2  // \$a = \$a * 2
\$a /= 2  // \$a = \$a / 2

Comparison Operators

Comparison operators compare two values, and return true or false.

// equal
1 == 1  //= true
1 == 2  //= false

// not equal
1 != 5  //= true
2 != 2  //= false

// greater/less than
2 > 1  //= true
1 < 5  //= true

// equal or greater/less than
2 >= 2  //= true
2 <= 0  //= false

// strings
'red' == 'red'   //= true
'red' != 'blue'  //= true
'red' == 'red '  //= false (extra space)
'red' == 'Red'   //= false (not exact case)

// different types are not equal
'0' == 0        //= false
'true' == true  //= false

Conditional Statements

if Statement

The if statement executes a block of code if the condition is true.

A block of code is surrounded by curly braces { }.

if condition {
// code to run if condition is true
}

For example:

if \$numCats > 100 {
print('It`s a cat-astrophe!')
}

if / else Statement

The else statement executes a block of code if the condition is false.

if condition {
// code if condition is true
}
else {
// code if condition is false
}

Example:

\$numGoldCoins = 30
\$tubaPrice = 33

if \$numGoldCoins >= \$tubaPrice {
print('You can buy the tuba! :)')
}
else {
\$needNumCoins = \$tubaPrice - \$numGoldCoins
print('You need ' ~ \$needNumCoins ~ ' more gold coins.')
}

//= 'You need 3 more gold coins.'

else if Statement

The else and if statements can be chained together.

Each condition is checked from top to bottom. Only the first true condition will be run.

if condition1 {
...
}
else if condition2 {
...
}
else if condition3 {
...
}
else {
// if all above conditions are false
}

Example:

\$score = 800
\$highScore = 1000

if \$score > \$highScore {
print('You set a new high score! :)')
}
else if \$score == 0 {
print('Worst score ever. :(')
}
else {
print('Try again. :|')
}

//= 'Try again. :|'

Logical Operators ||, &&

Logical operators let you join comparison operators.

Operator Name Description
&& AND BOTH conditions must be true
|| OR EITHER condition must be true
// And...
if \$ageYears < 13 && \$movieType == 'horror' {
print('This movie is too scary for you.')
}

// Or...
if \$heightCm < 120 || \$isAfraidOfHeights {
print('Do not enter this ride!')
}

Grouping

When mixing AND and OR expressions, use parens (...) to eliminate ambiguity.

Example:

if \$isAtRestaurant && (\$hasReservation || \$isFamous) {
print('You may enter the restaurant.')
}

NOT operator !

Precede an expression with ! to convert true to false and vice versa.

if !\$hasMagicShield {
print('The dragon eats you!')
}

Avoid negatives in your variable names, which can result in confusing double-negatives, which in turn can often lead to bugs.

// ✕ AVOID THIS

if !\$isNotEmployee {
print('Employee discount!')
}

Truthy/Falsey Values

As a shortcut, you can use a standalone value in place of a Boolean expression.

The following empty values are treated as false, and everything else is true:

Type Empty/False Value
Boolean false
Number 0
String ''
List []
Map {}

Examples:

// Boolean
\$isLoggedIn = true
if \$isLoggedIn {
print('Welcome back!')
}

// Number
\$numPosts = 0
if !\$numPosts {
print('Nothing here!')
}

// String
\$name = 'Tai'
if \$name {
print('Welcome back, ' ~ \$name)
}

// Lists
\$replies = []
if \$replies {
print(\$posts.length() ~ ' replies')
}