Array
The Array
type represents a list of values. Arrays in this language are similar to JavaScript arrays.
Syntax
Arrays are created using square brackets ([]
), with elements separated by commas:
const arr = [1, 2, 3];
Accessing Elements
You can access elements of an array using zero-based indexing:
const firstElement = arr[0]; // 1
const secondElement = arr[1]; // 2
Modifying Elements
Assign new values to array elements by using their index:
arr[1] = 5; // arr is now [1, 5, 3]
Length Property
Returns the number of elements in the array.
console.log(arr.length); // 3u
⚠️ The length property returns an unsigned integer!
Iteration
You can iterate over an array using a for loop
for(let i = 0; i < arr.length; i++)
console.log(arr[i]);
Or alternatively by using a ranged for loop
for(const elem : arr)
console.log(elem);
⚠️
elem
is a copy, so modifying it within the loop does not affect the original array