Skip to main content

Object

The Object type represents a collection of key-value pairs. Objects in this language are similar to JavaScript objects.

Syntax

Objects are created using curly braces ({}), with key-value pairs separated by commas. Keys must be strings (or string-like), and values can be of any type.

const obj = { key1: "value1", key2: "value2" };

Accessing and modifying elements

You can access values in an object using either dot notation or bracket notation.

Dot Notation

// read
const value1 = obj.key1; // "value1"

// write
obj.key1 = "newValue1";

Bracket notation

// read
const value2 = obj["key2"]; // "value2"

// write
obj["key2] = "newValue2";

⚠️ Only use these notations when you know that the element exists, otherwise an internal exception will be thrown! (see .contains())

Safely accessing elements

Check that the element exists by using the .contains() method.

if(obj.contains("key2"))
console.log(obj["key2"]);

Adding new properties

You can add new properties by using the .set() method.

obj.set("key3", "value3"); // add "key3" with the value of "value3".
obj.set("key3", "newValue3"); // if the key already exists, then it will edit the value instead.

Removing properties

You can remove properties by using the .remove() method.

const wasRemoved = obj.remove("key3"); // remove key3 if it exists

if(wasRemoved)
console.log("key3 was removed!");

Iteration

see the .keys() and .values() methods