List operators (JavaScript)

Lists can be processed as entities by certain operators.

The following operators work on lists:
Operation Description
list1 + list2 Returns list1 plus list2.
list1 - list2 Returns list1 minus list2.
list1 * list2 Returns list1 times list2.
list1 / list2 Returns list1 divided bylist2.

Usage

A list means a multi-value variable such as an array.

A list operation processes the corresponding elements of two lists, or each element of one list and a scalar value. The return value is a list.

If two list operands are not the same length, the last element of the shorter list fills out the operation.

Examples

This example adds the corresponding elements of two arrays.
function p(stuff) {
	print("<<<" + stuff + ">>>");
}

a = new Array(1, 2, 3);
b = new Array(0.1, 0.2, 0.3);
c = a + b;
p(c); // <<<1.1>>>,<<<2.2>>>,<<<3.3>>>

This example adds the corresponding elements of two arrays. The first array is shorter so its last element fills out the operation.

function p(stuff) {
	print("<<<" + stuff + ">>>");
}

a = new Array(1, 2, 3);
b = new Array(0.1, 0.2, 0.3, 0.4, 0.5);
c = a + b;
p(c); // <<<1.1>>>,<<<2.2>>>,<<<3.3>>>,<<<3.4>>>,<<<3.5>>>

This example adds a scalar value to each element of an array.

function p(stuff) {
	print("<<<" + stuff + ">>>");
}

a = new Array(1, 2, 3);
c = a + 0.01;
p(c); // <<<1.01>>>,<<<2.01>>>,<<<3.01>>>