Basics
let name = "Azkal"
let age = 25
let active = true
let nothing = null
Variables are declared with let. Types: number, string, boolean, null, array, object, function.
Functions
fn greet(name) {
return `Hello, {name}!`
}
// Default parameters
fn say(msg, times = 1) {
for i in range(times) { print(msg) }
}
// Anonymous functions
let add = fn(a, b) { return a + b }
Control Flow
if x > 0 { print("positive") }
else if x == 0 { print("zero") }
else { print("negative") }
while i < 10 { i = i + 1 }
for item in [1, 2, 3] { print(item) }
for ch in "hello" { print(ch) }
Template Strings
let name = "world"
print(`Hello, {name}!`)
print(`2 + 2 = {2 + 2}`)
Use backticks with {expr} for interpolation.
Objects
let user = {
name: "alky",
level: 99
}
user.name // "alky"
user["level"] // 99
| .keys() | Array of keys |
| .values() | Array of values |
| .has(key) | Check if key exists |
| .remove(key) | Delete a key |
Arrays
let nums = [1, 2, 3, 4, 5]
nums[0] // 1
nums.length // 5
[...a, ...b] // spread/merge
| .map(f) | Transform each element |
| .filter(f) | Keep matching elements |
| .reduce(f, init) | Combine into single value |
| .find(f) | First matching element |
| .includes(v) | Check if value exists |
| .sort() | Sort ascending |
| .reverse() | Reverse order |
| .flat() | Flatten nested arrays |
| .push(v) | Add to end |
| .pop() | Remove from end |
Strings
| .length | String length |
| .upper() | Uppercase |
| .lower() | Lowercase |
| .trim() | Remove whitespace |
| .split(sep) | Split into array |
| .contains(s) | Check substring |
| .replace(a, b) | Replace all occurrences |
| .starts_with(s) | Check prefix |
| .ends_with(s) | Check suffix |
| .chars() | Array of characters |
Destructuring
// Array destructuring
let [a, b, c] = [1, 2, 3]
// Object destructuring
let { name, age } = user
Try / Catch
try {
let result = 1 / 0
} catch (e) {
print(`Error: {e}`)
}
Comments
// Single-line comment
/* Multi-line
comment */
Pipe Operator
5 |> fn(x) { return x * 2 }
|> fn(x) { return x + 1 }
|> print // 11
Pipe |> passes the left value as the first argument to the right function.
Operators
| + - * / % | Arithmetic |
| == != | Equality |
| < > <= >= | Comparison |
| and or ! | Logical |
| |> | Pipe |
| ... | Spread |
Standard Library
| print(...args) | Output to console |
| type(val) | Get type as string |
| len(val) | Length of array/string/object |
| range(n) / range(a,b) / range(a,b,step) | Generate number array |
| num(val) | Convert to number |
| str(val) | Convert to string |
| bool(val) | Convert to boolean |
| join(arr, sep) | Join array into string |
| slice(val, start, end) | Slice array or string |
| append(arr, val) | Return new array with val |
| reverse(val) | Reverse array or string |
| keys(obj) | Object keys as array |
| values(obj) | Object values as array |
Math
| PI | 3.14159... |
| abs(n) | Absolute value |
| floor(n) / ceil(n) / round(n) | Rounding |
| sqrt(n) | Square root |
| pow(a, b) | Exponentiation |
| min(a, b) / max(a, b) | Min / Max |
| random() | Random 0-1 |
| clock() | Current time (ms) |