Javascript arrays do not need to have uniform type. So a single array may contain a mix of numbers, strings, objects, other arrays etc.
E.g.
let arr = ['cat', 5, 'dog'] // this is allowed
Javascript arrays can be accessed and modified using indexing notation.
E.g.
let arr = ['cat', 'dog', 'mouse'];
arr[0] // returns 'cat'
arr[0] = 'giraffe' // now arr[0] stores giraffe instead of cat
* Although strings behave like arrays in that they can be accessed with indexing notation, we cannot modify them using indexing or methods like push or pop on the strings like we do on arrays. Strings are immutable!
Common array properties:
- length - returns number of elements in the array E.g. var x = [1, 2, 3]; x.length -> 3
Common array modifiers: (*these functions modify the array objects which they are called with; no need to reassign to save change)
- sort([compare_func]) / reverse([compare_func]) - sorts the array in ascending / descending alphabetical order unless compare_func is given
- sorting function should take two parameters and return an integer < 0 if smaller, 0 if equal, and > 0 if greater.
- E.g. a typical numeric compare function would be function(a, b){return a - b}
- sorting function should take two parameters and return an integer < 0 if smaller, 0 if equal, and > 0 if greater.
- push([new_element]) - inserts the new element given to the end of the array
- pop() - returns the last element of the array and at the same time removes it from the array
- shift() - returns the first element of the array and at the same time shifts all subsequent elements one index lower, overwriting the first element
- unshift([new_element]) - shifts all elements in the array up one index and inserts the new element at index 0 and returns the new array length
- delete arr_x[index] - is equivalent to arr_x[index] = undefined; Doesn't work on entire array, only elements. E.g. delete arr_x doesn't do anything
- *fill([fill_value], [startInd], [endInd]) - fill the array with the fill value. Indices are optional - default startInd is 0, default endInd is array.length. The endInd is exclusive; if provided, the element at endInd itself won't be filled.
Queries: (*strings can use these methods as well, these do not modify the arrays/strings themselves)
- Array.isArray([variable_in_question]) - returns true if the variable given is an array (since typeof will return object for arrays and objects)
- This is available in ES5 and later, alternative ways to do this
- function isArray(x) { return x.constructor.toString().indexOf("Array") > -1;}
- x instanceof array will also return true if x is an array
- This is available in ES5 and later, alternative ways to do this
- indexOf([search_val], [startInd]) - similar to string's indexOf returns the lowest index of the search element if found, -1 if not found. An optional starting index parameter can be used to optimize the search
- lastIndexOf([search_val], [startInd]) - same as indexOf except in reverse order starting at the end of the array searching towards the beginning
- *find([bool_func]) - returns value of the first element which the boolean function returns true.
- the functions has the following signature: function([value], [index], [parent_arr]) the last two parameters are optional and can be omitted.
- *findIndex([bool_func]) - like find() but returns the index instead of value
- **includes([search_val], [startInd]) - returns true if the search element is found in the array, false if not. Optional parameter to indicate the starting search index.
Format:
- toString() - returns a string with each element of the array separated by a comma
- join([delimiter]) - returns a string with each element of the array joined by the given delimiter. The default delimiter is comma if unspecified.
- *keys() - returns an array iterator object consisting of keys or indices of the array.
- To iterate through this object, first assign iterator to variable E.g. var keys = arr.keys(); then use for... of ... E.g. for (x of keys) { do something with x}
- *entries() - returns an array iterator object consisting of key-value pairs
- To iterate through this object, first assign iterator to variable E.g. var entries = arr.entries(); then use for... of ... E.g. for (x of entries) { do something with x}
- x will be a comma-separated key-value pair
- To iterate through this object, first assign iterator to variable E.g. var entries = arr.entries(); then use for... of ... E.g. for (x of entries) { do something with x}
Common array methods: (*strings can use these methods as well, *these functions do not modify the array objects which they are called with; need to reassign to save change)
- splice([startInd], [nElemsToRemove], elemToAdd1, elemToAdd2...) - removes n elements starting at startInd, adds the additional elements at the startInd if given. The additional elements are optional.
- concat([arr_to_append1], [arr_to_append2]) - returns an array where the arr_to_append(s) are appened to the end of the array object with which the function is called. The parameters doesn't have to be strictly arrays, they can also be elements. E.g. arr_x.concat(arr_y) is legal, so is arr_x.concat(1) or arr_x.concat('apple');
- slice([startInd], [endInd]) - very much like the string's slice method which returns a substring starting at startInd and ending but exclusive of endInd, it returns a subarray starting at startInd, ending but excluding endInd element. The endInd is optional, and without it all subsequent elements will be in the subarray just like the string's slice. E.g. var arr = ['a', 'b', 'c']; arr.slice(1) -> ['b', 'c']. var str = "hello"; str.slice(1) -> "ello".
Common iteration methods: (*strings cannot use these methods; these functions do not execute on the indices which do not have values i.e. holes in the array, also these methods do not modify the original, must be reassigned to be meaningful. Supported in IE9 and later)
- foreach([function]) - passes each element to the function given as the value parameter.
- the functions has the following signature: function([value], [index], [parent_arr]) the last two parameters are optional and can be omitted. The function returns undefined and this is intentional
- map([function]) - creates a new array and then performs the function on each element of the array
- the function has the same signature as foreach however the function should return something as the new value to be written to the new array
- filter([bool_func]) - creates new array and then only adds the element from the original to the new one if the given function returns true
- the function has the same signature as foreach however the function should return a boolean to act as a filter whether the element "passes" or "gets filtered"
- reduce([acc_func], [initial_value]) - goes through the array and returns a single value
- the acc_func (acc is short for accumulator) has the following signature: function([total], [value], [index], [parent_arr]). The last two parameters can be omitted. The "total" is an accumulator element that keeps track of "total" so far.
- The function needs a return statement, usually this will include the accumulator variable "total"
- The reduce method takes optional initial_value parameter which will be assigned to the accumulator "total" in the beginning, by default the accumulator is initialized with the first element in the array
- reduceRight([acc_func], [initial_value]) - the same as reduce but starts at the end of the array
- every([bool_func]) - like filter() but returns a single boolean that is the result of logical-AND of all the elements evaluated through the boolean function
- some([bool_func]) - like every() but returns the logical-OR of all the elements evaluated through the boolean function
* Recent feature: IE12 and later (please check other browser versions before using)
** Recent feature: ES7, IE14 and later (please check other browser versions before using)