Skip to main content

Arrays

An array is a special variable, which can hold more than one value at a time. It is essentially a list that holds multiple values. For example:

var array = [1, 2, 3, 4, 5]

Arrays have many many benefits over using a single variable for every value. What if you had hundreds of values? With arrays, you can store all of the values neatly. With arrays, you have much more functionality too, such as iterating over the values.

The syntax for creating an array is:

[var | const] array = [item1, item2, ...]

You can also have empty arrays such as [].

Note: indices in LemonScript start from 0, so the first character of a string will have index 0, and the second 1, etc

Static Properties#

  • length - The length property describes how many elements are in the array. For example, [1, 3, 2, 5, 6.6].length is 5.

Methods#

  • copy() - Creates a duplicate and a new instance of the array.

  • concat(array) - Concats the array with another specified array. For example [1, 2, 3].concat([4, 5, 6]) returns [1, 2, 3, 4, 5, 6].

  • contains(value) - Returns a boolean indicating whether the specified value is in the array. For example, [true, true, false, 5].contains(5) returns true.

  • get(index) - Returns the value at the given index. For example, ["Hello", "world"].get(1) returns "world".

  • index(value) - Returns the index of the first occurrence of the given value in the array. If no occurrences are found, then the method returns null. For example, ["Hello", "world"].index("world") returns 1.

  • insert(index, value) - Inserts the value at the given index in the array.

  • join(joiner) - Returns a string with all the elements joined together using a specified string. For example, ["I", "am", "Bob"].join(" ") returns "I am Bob".

  • pop() - The pop method removes the last element from an array and returns it. If there are no elements to pop, it returns null. For example, ["red", "blue", "green", "yellow"].pop() returns "yellow", and the array becomes ["red", "blue", "green"].

  • push(value1<, value2, value3, ...>) - The push method pushes all the specified elements to the end of the array. For example, [1, 2, 3].push(4) changes to array to [1, 2, 3, 4]. You can also specify more than one element: [1, 2, 3].push(4, 5, 6) becomes [1, 2, 3, 4, 5, 6].

  • remove(value) - Removes all occurrences of a given value from the array. For example, ["red", "blue", "green", "yellow", "red"].remove("red") changes the original array to ["blue", "green", "yellow"].

  • reverse() - Returns the reverse of the array. For example, [1, 2, 3].reverse() returns [3, 2, 1].

  • set(index, value) - Sets a value in the array at the given index to the specified value.

  • shuffle() - Returns the array but with its elements shuffled and in a random order.

  • slice(min<, max>) - Returns a new array containing the specified range of elements. For example, [1, 2, 3, 4, 5].slice(1, 3) returns [2, 3]. If no max argument it specified, the output will be the sliced array from min to the end of the array. You can also use negative indices to slice from the end of the array. For example, [1, 2, 3, 4, 5].slice(1, -2) returns [2, 3].

  • sort() - The sort method sorts the array in ascending order. For example, [5, 3, 1, 2].sort() returns [1, 2, 3, 5], and ["1", "2", "11", "a", "c", "b"].sort() returns ["1", "11", "2", "a", "b", "c"]. If there are mixed types in the array, then it'll sort the types, and place them in this order: [numbers, strings, nulls, falses, trues]. E.g [1, 3, "b", 2, false, "a", null, true].sort() returns [1, 2, 3, "a", "b", null, false, true].