Beat Signer - Department of Computer Science -
[email protected] 18
October 29, 2024
Arrays
▪ An array is a variable that can hold multiple values which
can be accessed via an integer offset
▪ values of different types might be mixed
▪ Other methods to sort arrays, combine arrays etc.
const points = [10, 5, 17, 42];
points.length; // number of elements (4)
points[1]; // accesses the element with the given offset (5)
points[points.length] = 13; // adds 13 to the end of the array [10, 5, 17, 42, 13]
let last = points.pop(); // removes the last element [10, 5 , 17 , 42]
let l = points.push(2); // adds 2 at the end and returns the length (5)
let first = points.shift(); // removes the first element [5, 17, 42, 2]
l = points.unshift(7); // adds 7 to the beginning and returns the length (5)
delete points[2]; // the third element will be undefined
const matrix = [[3, 1, 2], [9, 6, 4], [5, 7, 8]]; // array of arrays ("2D" array)
matrix [1][2]; // 4