Language

Arrays & Strings

Arrays are 0-indexed and support a rich set of built-in methods including map, filter, and reduce. Strings are UTF-8 and have a matching method set.

Arrays

let nums  = [1, 2, 3, 4, 5]
let mixed = [42, "hello", true]
let empty = []

Typed arrays

Put a type between the variable name and = to enforce the element type:

let scores [int]    = [10, 20, 30]
let prices [decimal] = [9.99, 14.50]
let names  [string]  = ["Ana", "Bob"]

scores.push(40)        // ✅
scores.push("hello")   // ❌ TYPE ERROR: Cannot push 'string' into [int] array

Accessing and mutating

let nums = [10, 20, 30]

out nums[0]    // → 10
out nums[2]    // → 30

nums[1] = 99   // mutate by index
out nums       // → [10, 99, 30]

out nums.length   // → 3 (property, no parentheses)

Add and remove elements

let arr = [1, 2, 3]

arr.push(4)      // add to end → [1, 2, 3, 4]
arr.unshift(0)   // add to front → [0, 1, 2, 3, 4]

let last  = arr.pop()    // remove from end → returns 4
let first = arr.shift()  // remove from front → returns 0

arr.remove(1)   // remove at index 1

Search and query

let nums = [1, 2, 3, 4, 5]

out nums.indexOf(3)          // → 2
out nums.includes(99)        // → false
out nums.find(x => x > 3)   // → 4  (first match)
out nums.findIndex(x => x > 3)  // → 3

out nums.every(x => x > 0)  // → true  (all match)
out nums.some(x => x > 4)   // → true  (at least one matches)

Transform

let nums = [1, 2, 3, 4, 5]

// map — transform each element, returns new array
let doubled = nums.map(x => x * 2)
out doubled   // → [2, 4, 6, 8, 10]

// filter — keep matching elements, returns new array
let evens = nums.filter(x => x % 2 == 0)
out evens   // → [2, 4]

// reduce — fold into a single value
let sum = nums.reduce(0, (acc, x) => acc + x)
out sum   // → 15

// Chain them
let result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    .filter(x => x % 2 == 0)
    .map(x => x * x)
    .reduce(0, (acc, x) => acc + x)
out result   // → 220

Sort

let nums = [5, 2, 8, 1, 4]

nums.sort()           // ascending → [1, 2, 4, 5, 8]
nums.sort("desc")     // descending → [8, 5, 4, 2, 1]

// Custom comparator — positive result = swap (same as JS)
let byLength = ["banana", "apple", "fig"]
byLength.sort((a, b) => a.length - b.length)
out byLength   // → [fig, apple, banana]

Other useful methods

let nums = [1, 2, 3, 4, 5]

out nums.slice(1, 4)   // → [2, 3, 4]  (start inclusive, end exclusive)
out nums.join(", ")    // → "1, 2, 3, 4, 5"
out nums.join()        // → "1,2,3,4,5"

nums.reverse()         // mutates in-place

let nested = [[1, 2], [3, 4]]
out nested.flat()      // → [1, 2, 3, 4]

Strings

Basic operations

let s = "hello world"

out s.length           // → 11
out s.toUpperCase()    // → HELLO WORLD
out s.toLowerCase()    // → hello world
out s.trim()           // removes leading/trailing whitespace

// Concatenation and repetition
out "hello" + " world"   // → hello world
out "ha" * 3             // → hahaha

Searching

let s = "hello world"

out s.includes("world")     // → true
out s.indexOf("world")      // → 6
out s.startsWith("hello")   // → true
out s.endsWith("world")     // → true

Extracting parts

let s = "hello world"

out s.substring(0, 5)   // → hello
out s.slice(6)          // → world  (from index 6 to end)
out s.slice(-5)         // → world  (last 5 characters)
out s.charAt(4)         // → o

Modifying

let s = "one two one two"

out s.replace("one", "X")   // → X two X two  (replaces all occurrences)
out s.split(" ")             // → [one, two, one, two]
out "abc".split("")          // → [a, b, c]

// Padding
out "42".padStart(5, "0")   // → 00042
out "hi".padEnd(5, "-")     // → hi---