len()
The len() built-in function returns the length or size of a value. It works with arrays, strings, and objects.
Syntax
len(value)Array length
Returns the number of elements in an array:
let arr = [10, 20, 30];
print(len(arr)); // 3
let empty = [];
print(len(empty)); // 0String length
Returns the number of characters in a string:
let s = "hello";
print(len(s)); // 5
let empty = "";
print(len(empty)); // 0Object field count
Returns the number of fields in an object:
class Point {
let x = 10;
let y = 20;
let z = 30;
}
let p = new Point();
print(len(p)); // 3Common use case
Iterating with index using len():
let arr = ["a", "b", "c"];
@unsafe for (let i = 0; i < len(arr); i = i + 1) {
print("Index", i, ":", arr[i]);
}Error handling
⚠️ Calling len() on unsupported types (like integers or booleans) throws a runtime error.