JavaScript Arrays Flashcards
What is an Array?
Definition
The JavaScript Array object is a global object that is used in the construction of arrays; which are high-level, list-like objects.
Array Syntax
var fruits = [‘Apple’, ‘Banana’]; console.log(fruits.length); // 2
var first = fruits[0]; // Apple var last = fruits[fruits.length - 1]; // Banana
What does index mean?
The count starts at 0
Are arrays indexed?
Yes
An example of an idexed array…
var example = [1 , 12, “Aaron”, 4, “Scott”, true]
example [2]
returns Aaron
What is push?
Add something to the end of an array.
colors.push (“blue”);
What is a pop?
Add something to the begining of an array.
colors.pop (“blue”);
Whst is a shift in an array?
Remove something from the begining of an array.
colors.shift (“blue”);
What is a Unshift in an array?
Add something to the begining of an array.
colors.uhshift (“blue”);
What is indexOf in an array?
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
What is the syntax of indexOf in an array?
var a = [2, 9, 9];
a. indexOf(2); // 0
a. indexOf(7); // -1
What is slice in an array?
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.
What is the syntax of slice in an array?
var a = [‘zero’, ‘one’, ‘two’, ‘three’];
var sliced = a.slice(1, 3);
console.log(a); // [‘zero’, ‘one’, ‘two’, ‘three’] console.log(sliced); // [‘one’, ‘two’]
What is a nested array?
Arrays can be nested, meaning that an array can contain another array as an element. Using this characteristic of JavaScript arrays, multi-dimensional arrays can be created.
What is the syntax of a nested array?
var a = new Array(4);
for (i = 0; i < 4; i++) {
a[i] = new Array(4);
for (j = 0; j < 4; j++) {
a[i][j] = ‘[’ + i + ‘, ‘ + j + ‘]’;
}
}