1 + 2 # 3
3 - 1 # 2
6 / 3 # 2
3 * 2 # 6
5 % 2 # 1
2 ** 3 # 8
-i # Unary negation
+i # Unary plus
null && 10 # null
0 && 10 # 0
1 && 10 # 10
'' && 'str' # ''
The logical AND operator is true if all of its operands are true. The operator returns the value of the last truthy operand.
null and 10 # null
1 and 10 # 10
Alias for &&
operator
null || 10 # 10
0 || 10 # 10
1 || 10 # 1
The logical OR operator is true if one or more of its operands is true. The operator returns the value of the first truthy operand.
null or 10 # 10
0 or 10 # 10
1 or 10 # 1
Alias for ||
operator
null ?? 10 # 10
0 ?? 10 # 0
'' ?? 'str' # ''
The nullish coalescing operator ??
is a logical operator that returns its right-hand side operand when its left-hand side operand is null
or undefined
, and otherwise returns its left-hand side operand.
let a = true
!a # false
!10 # false
!0 # true
x == y # Equality
x != y # Inequality
x === y # Strict equality
x is y # Also strict equality
x !== y # Strict inequality
x isnt y # Also strict inequality
x > y # Greater than
x >= y # Greater than or equal
x < y # Less than
x <= y # Less than or equal
honda isa Car #
The isa
operator tests whether the prototype property of a constructor appears anywhere in the prototype chain of an object. Alias for the javascript instanceof operator.
princess !isa Car
typeof item
a = b
a ||= b # If falsy assignment
a &&= b # If truthy assignment
a ??= b # If null assignment
a += b # Addition assignment
a -= b # Decrement assignment
a *= b # Multiplication assignment
a /= b # Division assignment
a %= b # Remainder assignment
a **= b # Exponential assignment
a++ # Increment assignment, returns original value
a-- # Decrement assignment, returns original value
++a # Increment assignment, returns incremented value
--a # Decrement assignment, returns decremented value
let object = {}
let input = 200
# ---
if object.value =? input
yes
Regular assignment that returns true or false depending on whether the left-hand was changed or not. More concise way of doing:
let object = {}
let input = 200
# ---
if object.value != input
object.value = input
yes
The reassignment may seem unnecessary at first, but since memoization is an oft-used pattern in Imba, this is a very convenient addition.
a & b # Bitwise AND
a !& b # Bitwise NOT AND
Essentially the same as
(a & b) == 0
a | b # Bitwise OR
a ^ b # Bitwise XOR
~ a # Bitwise NOT
a << b # Left shift
a >> b # Sign-propagating right shift
a >>> b # Zero-fill right shift
a <<= 1 # Left shift assignment
a >>= 1 # Right shift assignment
a >>>= 1 # Unsigned right shift assignment
a &= 1 # Bitwise AND assignment
a |= 1 # Bitwise OR assignment
a ~= 1 # Bitwise NOT assignment (unassignment)
a ^= 1 # Bitwise XOR assignment
const STATES = {LOADED: 2}
let data = {state: 0}
# ---
if data.state |=? STATES.LOADED
yes
Bitwise OR assignment that returns true only if the bit(s) was not previously set. Essentially a concise way to do
const STATES = {LOADED: 2}
let data = {state: 0}
# ---
if (data.state & STATES.LOADED) == 0
data.state |= STATES.LOADED
# do something here...
const STATES = {LOADED: 2}
let data = {state: 0}
# ---
if data.state ~=? STATES.LOADED
# went from loaded to not loaded
Bitwise unassignment that unsets the right-hand bits from left-hand value and returns true / false depending on whether this actually changed the left-side or not.
const STATES = {LOADED: 2}
let data = {state: 0}
# ---
if (data.state & STATES.LOADED) == 0
data.state |= STATES.LOADED
# do something here...
a ^=? 1 # Bitwise XOR assignment
Imba uses ..
for optional chaining. If the optional reference is nullish it will return undefined.
let object = {one: {value: 1}}
console.log object..two..value
# undefined
console.log object.two.value
# TypeError: Cannot read property 'value' of undefined
let object = {one: 1, two: 2}
delete object.one
class Game
turn
tiles
moves
winner
def constructor
moves = []
tiles = new Array(9)
turn = 0
switch status
when "completed"
console.log "This project has been completed"
when "archived"
console.log "This project has been archived"
else
console.log "This project is active"
for num in [1,2,3]
num * 2
for item in items
<li> item.name
for item, i in items
<li> "{i+1}: {item.name}"
class Item
price = 100
taxRate = 20
get totalPrice
price * (1 + taxRate / 100)
def render
<self>
<input bind=price>
<input bind=taxRate>
<p> "Total: {totalPrice}" # 120
class Item
set name value
console.log "The name has been set to", value
def multiply a, b
a * b
# default values
def method name = 'imba'
console.log param
# destructuring parameters
def method name, {title, desc = 'no description'}
console.log name,title,desc
<form attr:id="product-form">
# Define a new global tag component
tag page-header
...
# Define a new local tag component
tag Header
...
if condition
console.log 'yes!'
if expr > 10
console.log 'over 10'
elif expr > 5
console.log 'over 5'
elif expr
console.log 'not falsy'
if condition
console.log 'yes!'
else
console.log 'no!'
def fetch
# adding a try without a catch block will silently swallow an erro
try
const result = await axios.get('my-api.com')
def fetch
try
const result = await axios.get('my-api.com')
catch e
console.error "There was an error", e
let res = for num in [1,2,3,4,5]
continue if num == 3
num * 2
console.log res # [2,4,8,10]
# continue with an argument acts like early return within Array#map
let res = for num in [1,2,3,4,5]
continue -1 if num == 3
num * 2
# res => [2,4,-1,8,10]
let res = for num in [1,2,3,4,5]
break if num == 3
num * 2
# res => [2,4]
# When supplying an argument to break
# this value will be added to the resulting array
let res = for num in [1,2,3,4,5]
break -1 if num == 3
num * 2
# In Imba the last statement is returned automatically
def add a, b
a + b
# But it can be useful for returning other values or early
def add a, b
return 0 unless a && b
a + b