const LIMIT = 10; const NAME = "dusk"; const FLAG = true; const NOTHING = null; const RATIO = 2.5; assert(LIMIT == 10); assert(NAME == "dusk"); assert(FLAG); assert(isnull(NOTHING)); assert(RATIO * 2 == 5); assert(LIMIT + 1 == 11); assert(NAME + "!" == "dusk!"); assert(len(NAME) == 4); var total = 0; for(var i = 0; i < LIMIT; i++) total += 1; assert(total == LIMIT); // Constant arrays and structs can be read const LIST = [1, "two", [3, 4], {five: 5}]; assert(len(LIST) == 4); assert(LIST[0] == 1); assert(LIST[1] == "two"); assert(LIST[2][1] == 4); assert(LIST[3].five == 5); const CONFIG = {name: "cfg", nested: {depth: 2, items: [10, 20]}, on: true}; assert(CONFIG.name == "cfg"); assert(CONFIG.nested.depth == 2); assert(CONFIG.nested.items[1] == 20); assert(CONFIG["nested"]["items"][0] == 10); assert(CONFIG.on); assert(isundefined(CONFIG.missing)); foreach(var item : LIST) assert(!isundefined(item)); assert(join(map(CONFIG.nested.items, function(v) { return v; }), ",") == "10,20"); assert(indexof(LIST, 1) == 0); // Constant functions function helper(n) { return n + LIMIT; } assert(helper(1) == 11); // Copies of constants can be changed, the constant itself stays as it was var copy = clone(LIST); push(copy, "extra"); copy[0] = "changed"; assert(len(copy) == 5 && copy[0] == "changed"); assert(len(LIST) == 4 && LIST[0] == 1); var deep = deepclone(CONFIG); deep.name = "other"; deep.nested.items[0] = 99; assert(CONFIG.name == "cfg"); assert(CONFIG.nested.items[0] == 10); assert(deep.nested.items[0] == 99); var fromConst = LIMIT; fromConst++; assert(fromConst == 11); assert(LIMIT == 10); // A constant in a function that is called more than once function useName() { return NAME + "?"; } assert(useName() == "dusk?"); assert(useName() == "dusk?"); // A constant built from other constants const DOUBLE = LIMIT * 2; assert(DOUBLE == 20); const GREETING = NAME + " " + "engine"; assert(GREETING == "dusk engine");