Skip to main content

Control Flow

Control flow statements allow you to control the execution order of your code.

Boolean convertible types

The following types can be used as a condition

Always false

false when 0

⚠️ Any other type will throw a runtime error!

Conditional Statements

if Statement

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

if (condition) {
// Block of code
}

else Statement

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

if (condition) {
// Block of code if condition is true
} else {
// Block of code if condition is false
}

else if Statement

The else if statement is used to specify multiple conditions.

if (condition1) {
// Block of code if condition1 is true
} else if (condition2) {
// Block of code if condition2 is true
} else {
// Block of code if no conditions are true
}

Loops

for Loop

The for loop repeats a block of code a specific number of times, based on the condition provided.

for (let i = 0; i < 10; i++) {
// Block of code to repeat
}

ranged-for

The ranged-for loop iterates over an iterable type (string, array)

const arr = [1,2,3];
for(const element : arr) {
//repeat 3 times
}

while Loop

The while loop executes a block of code as long as the specified condition is true.

while (condition) {
// Block of code to repeat
}

repeat Loop

The repeat loop executes a block of code forever.

repeat {
// Block of code to repeat
}

repeat...while Loop

The repeat...while loop executes a block of code once, and then repeats it as long as the specified condition is true.

repeat {
// Block of code to repeat
} while(condition);

match Statement

The match statement provides conditional branching similar to switch in other languages, but with clearer and safer semantics:

  • No fallthrough: Only one matching case is executed.
  • Scoped cases: Each case introduces a new block scope.
  • Optional default: Runs when no other case matches.
match (expression) {
case value1:
// code for value1

case value2:
// code for value2

default:
// code if no case matches
}
  • Values are compared using strict equality (===).
  • Each case block is isolated; variables declared inside do not leak outside.

Control Flow with break and continue

break

The break statement exits a loop or switch statement early, skipping any remaining iterations.

for (let i = 0; i < 10; i++) {
if (i === 5) {
break; // Exits the loop when i equals 5
}
console.log(i);
}

continue

The continue statement skips the current iteration of a loop and proceeds to the next iteration.

for (let i = 0; i < 10; i++) {
if (i === 5) {
continue; // Skips the iteration when i equals 5
}
console.log(i);
}

Conditional Shortcuts

Varjus supports conditional shortcut evaluation with logical operators, just like JavaScript.

  • || (OR): If the left-hand side is truthy, the right-hand side is not evaluated.
  • && (AND): If the left-hand side is falsy, the right-hand side is not evaluated.
const a = true || someFunction();  // someFunction is not called
const b = false && otherFunction(); // otherFunction is not called

This behavior can be useful for fallback values or guarding expressions.