Arrays Flashcards
let arr =
let arr =
write two ways for empty array
new Array();
[]
let fruits = [“Apple”, “Orange”, “Plum”];
fruits[2] = ‘Pear’; // returns
[“Apple”, “Orange”, “Pear”]
let fruits = [“Apple”, “Orange”, “Plum”];
// how to insert ‘lemon’ at the end?
now [“Apple”, “Orange”, “Pear”, “Lemon”]
fruits[3] = ‘Lemon’;
let fruits = [“Apple”, “Orange”, “Pear”];
alert( fruits.pop() );
alert( fruits ); // returns?
remove “Pear” and alert it
Apple, Orange
let fruits = [“Apple”, “Orange”];
fruits.push(“Pear”);
alert( fruits ); // returns?
Apple, Orange, Pear
let fruits = [“Apple”, “Orange”, “Pear”];
alert( fruits.shift() );
alert( fruits ); // returns?
Orange, Pear
let fruits = [“Orange”, “Pear”];
fruits.unshift(‘Apple’);
alert( fruits ); // returns?
Apple, Orange, Pear
let fruits = [“Apple”];
fruits.push(“Orange”, “Peach”);
fruits.unshift(“Pineapple”, “Lemon”);
// returns?
[“Pineapple”, “Lemon”, “Apple”, “Orange”, “Peach”]
alert( fruits );
let fruits = [“Banana”]
let arr = fruits; // copy by reference (two variables reference the same array)
alert( arr === fruits ); // returns and why?
true
there are only eight basic data types in JavaScript (see the Data types chapter for more info). Array is an object and thus behaves like an object
let fruits = [“Banana”]
let arr = fruits;
arr.push(“Pear”);
alert( fruits ); // returns?
Banana, Pear - 2 items now
One of the oldest ways to cycle array items is the for loop over indexes:
But for arrays there is another form of loop, _______
for..of:
Generally, we shouldn’t use what iteration for arrays.
for..in
let arr = [1, 2, 3, 4, 5];
the simplest way to clear the array is:
arr.length = 0;.
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
alert( matrix[1][1] ); //
5, the central element
let arr = [1, 2, 3];
alert( arr ); // 1,2,3
alert( String(arr) === ‘1,2,3’ ); // returns and why?
true
Arrays have their own implementation of toString method that returns a comma-separated list of elements.