Arrays in JavaScript are a special kind of object that inherits from the Array.prototype. Arrays are ordered non-typed dynamic collections of values.
Creating an array
Using the constructor syntax:
Using the array literal syntax:
Using Array.of():
Array.from() creates arrays from array-like objects (HTMLCollection, NodeList, Arguments object) and
iterable objects (String, Array, TypedArray, Map and Set are built in iterables.).
Here we are creating a new array with only unique values
JavaScript does not have associative arrays. If you are using a named index you are setting or accessing a variable associated with that array’s object property collection, which is separate from the list of array elements. You cannot use array’s traversal and mutation operations with these named properties.
Accessing elements
Adding, removing and replacing elements
We have the following array
We can add elements to the end of the array with push(). Here we are
adding only one element but you can add as many as you wish by separating
the values with commas.
To add elements to the start of the array you use unshift().
pop() is used to remove an element from the end of the array, it returns the removed element.
shift() is used to remove an element from the start of the array, it returns the removed element.
If you need to add, remove or replace elements in other indexes, you can use splice().
The delete operator removes the contents of the element and leaves the element empty. If you wish to remove the element completely, you should use splice().
Making a copy of an array
Arrays are passed by referefence.
To make a copy of an array, you can use the following methods.
If you now change the original array the copies are not affected.
Array properties
length
The length property returns the the last index number of the array + 1.
Static array methods
Array.from()
Array.from(arrayLike[, mapFn[, thisArg]])
The Array.from() method creates a new shallow-copied Array instance from an array-like or iterable object.
The arguments for Array.from() are the array-like object, optionally a mapping function to use on the elements, and optionally a this value to use when executing the mapping function.
Array.of()
Array.of(element0[, element1[, ...[, elementN]]])
The Array.of() method creates an array from a list of values. The
difference between Array.of() and the array contructor is that when
you pass a single number Array.of() creates an array with a single
element which is that number whereas the array contructor creates
an array with that many empty elements.
Array.isArray()
The Array.isArray() method checks if a value is an array and returns
true or false.
Array prototype methods
concat()
array.concat([value1[, value2[, ...[, valueN]]]])
The concat() method merges two or more arrays and returns the new merged array. It does not recurse into nested array arguments and it does not mutate the original array. If no arguments are passed concat() returns a shallow copy of the original array.
copyWithin()
copyWithin(target, [start[, end]])
copyWithin() is used to shallowcopy values within an array. It is a mutator method that does not change the length of the array. Arguments are target (where to copy) and optionally start and end index (from where to copy). End index is not included in the copy. Return value is the modified array.
entries()
The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array. The method does not take any arguments.
every() and some()
every() method takes a function that tests the elements in the array for a condition and returns true or false for each element. If the condition matches every element in the array or the array is empty true is returned. some() works in a similar way: true is returned if the condition matches at least one element in the array. For empty arrays some() returns false. The functions are not called on empty elements.
Empty array
To test if none of the elements match
fill()
The fill() method fills an array or parts of an array with a given value. It is a mutator method. Arguments are the fill value, optional start index and also optional end index (the value is filled up to [not including] end index). Return value is the modified array.
fill(value, [start[,end]])
find() and findIndex()
find() method returns the value of the first element in the array that matches the condition provided by the callback function. findIndex() works the same but instead of the value it returns the index of the element.
flat()
The flat() method flattens an array and returns a new array. If the array contains empty elements, those are removed. It does not mutate the original array. It takes one parameter which is the depth and if no parameter is given the default depth of 1 is used.
flatMap()
The flatMap() method is identical to a map() followed by a flat() with a depth of 1.
forEach()
arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
The forEach() method executes the provided function for each array element in ascending order. It is not invoked for index properties that have been deleted or are uninitialized. You can only break out of a forEach() loop by throwing an exception. The return value of forEach() is always undefined. forEach() does not mutate the original array, unless you do something inside the callback that mutates it.
includes()
includes() method is used to check if an array contains the given value. It only takes one argument which is the value that is checked.
indexOf() and lastIndexOf()
indexOf() method returns the index of the first element in the array that
matches the given value. lastIndexOf() returns the index of the last
element that matches the given value. If no element matches -1 is returned.
join()
join() method joins all the elements in the array and returns the result as a string. It does not mutate the original array. If the element value is undefined, null or an empty array [] it is converted to an empty string.
join() takes one argument which is a string that separates the elements
in the returned value. The elements are separated with commas by default if no argument is given.
keys()
The keys() method returns a new Array Iterator object that contains the keys for each index in the array. The method does not take any arguments. Keys for empty elements are also included.
The map() method takes a function that goes through each element in an array and returns these processed elements in another array. The function is not called for empty elements.
The filter() method is used to filter elements from the array that match a certain criteria. It takes a predicate function as a callback which is run for each element in the array. The function should return true for elements which will be kept, false otherwise. It returns a new array that contains only the matching elements. It does not mutate the original array.
The reduceRight() method reduces the elements in an array into one value from right to left.
push() and pop()
push()
The push() method is used to add one or more elements to the end of the array. Return value is the new length of the array.
pop()
The pop() method removes the last element from the array. It does not take any arguments. Return value is the removed element.
reverse()
reverse() method reverses an array in place and returns a reference to the array. It does not take any arguments.
If you do not wish to mutate the original array, you can create a shallow copy of the original with slice().
sort()
sort() method sorts an array in place and returns the sorted array. It takes an optional compare function as argument. If no compare function is given as argument, then elements are converted to and sorted as strings in alphabetical and ascending order by default.
Default sort order
Using a compare function
The number returned by the compare function determines the sort order.
Return values less than 0 sort the first argument before the second.
Return value of 0 keeps the order.
Return values larger than 0 sort the second argument before the first.
Sorting strings by length
slice()
slice([begin[, end])
slice() method creates a shallow copy of a part of an array and returns the copy as a new array. It does not mutate the original array. It takes the begin and end index (end index is not included in the new array) as argument. If end index is omitted, slice() copies to the end of the array. A negative index can be used, indicating an offset from the end of the array.
splice() method is used to remove and replace elements in an array. It mutates the original array. First argument is the start index and the second argument is the number of elements to remove from the array (the default is all from the start index to the end of the array). Rest of the arguments are new values you want to add to the array. Return value is the removed elements in a new array or [0] if no elements were removed.
toLocaleString()
array.toLocaleString([locales[, options]])
toLocaleString() returns a string representing the elements of the array.
the elements are converted to strings using their tolocalestring methods and these strings are separated by a locale-specific string (such as a comma “,”). If locale is not defined then the default system locale is used. tolocalestring() does not mutate the original array.
toString()
toString() returns a string representation of the array where the elements of the array are separated with commas.
It does not mutate the original array. toString() does not take any arguments.
unshift() and shift()
unshift()
The unshift() method is used to add one or more elements to the start of the array. Return value is the new length of the array.
shift()
The shift() method removes the first element from the array. It does not take any arguments. Return value is the removed element.
values()
The values() method returns a new Array Iterator object that contains the values for each index in the array. The method does not take any arguments.