Control flow
If/Else
How JavaScript's if/else if/else and ternary operator compare to Go (no ternary), Rust (if is an expression), and Swift/Java (both have a ternary).
- Minimum versions
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 2.0Java ≥ 25
- Verified on
- Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1
Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.
if/else if/else in Go reads almost identically to JavaScript — you just drop the parentheses around the condition. The biggest difference: Go has no ternary operator (? :), so you write it out with a variable and a plain if block instead. Rust also has no ternary operator, but if in Rust is an expression, so it reads more compactly than Go's version. Swift and Java both have a real ? : just like JavaScript.
Conditional branching
const array = [1, 2]
if (array) {
console.log('array exists')
}
if (array.length === 2) {
console.log('length is 2')
} else if (array.length === 1) {
console.log('length is 1')
} else {
console.log('length is other')
}
const isOddLength = array.length % 2 == 1 ? 'yes' : 'no'
console.log(isOddLength)package main
import "fmt"
func main() {
array := []byte{1, 2}
if array != nil {
fmt.Println("array exists")
}
if len(array) == 2 {
fmt.Println("length is 2")
} else if len(array) == 1 {
fmt.Println("length is 1")
} else {
fmt.Println("length is other")
}
// closest thing to a ternary operator
isOddLength := "no"
if len(array)%2 == 1 {
isOddLength = "yes"
}
fmt.Println(isOddLength)
}fn main() {
let array: Vec<u8> = vec![1, 2];
// an owned Vec has no "nil" state like a Go slice, so it always "exists" here
println!("array exists");
if array.len() == 2 {
println!("length is 2");
} else if array.len() == 1 {
println!("length is 1");
} else {
println!("length is other");
}
// `if` is an expression in Rust — closer to a ternary than Go's approach
let is_odd_length = if array.len() % 2 == 1 { "yes" } else { "no" };
println!("{is_odd_length}");
}let array = [1, 2]
// Array is a value type and non-optional here, so it always "exists" — no nil state like a Go slice
print("array exists")
if array.count == 2 {
print("length is 2")
} else if array.count == 1 {
print("length is 1")
} else {
print("length is other")
}
let isOddLength = array.count % 2 == 1 ? "yes" : "no" // Swift has a ternary operator, unlike Go
print(isOddLength)void main() {
int[] array = { 1, 2 };
if (array != null) {
IO.println("array exists");
}
if (array.length == 2) {
IO.println("length is 2");
} else if (array.length == 1) {
IO.println("length is 1");
} else {
IO.println("length is other");
}
String isOddLength = array.length % 2 == 1 ? "yes" : "no"; // Java has a ternary operator, unlike Go
IO.println(isOddLength);
}array exists
length is 2
noReference: github.com/miguelmota/golang-for-nodejs-developers#ifelse