Quick Intro to Arrays in JavaScript
Arrays
- get first item in an array:
array[0]
- get last item in an array:
array[array.length-1]
- add item to end:
array.push(val)
can push multiple values - add item to beginning:
array.unshift(val)
- remove item from end:
array.pop()
returns the last item from an array and removes it - remove item from start:
array.shift()
returns the first item from an array and removes it - Join elements in an array:
.join()
- Join two Arrays:
.concat()
- Search arrays:
indexOf()
Loops for Arrays
for( var i=0; i<arr.length; i += 1) {
console.log(arr[i]);
}
Do note that using for
loops are the ancient way of doing things. You should be using cool higher order functions like .map
and .reduce