Intro

In JavaScript array is a list-like object used to store multiple data in a single container or variable.

Array items are represented by zero-based indexes, 0 is the first item, 1 is the second, 2 is the third and so on...

Array Properties

An array has three main properties, constructor, length, and prototype.

constructor

Returns the function that created the array object's prototype.

length

The length property can either set or return the length or total number of elements in an array object.

// Set the new array length to 10
new Array().length = 10;
const animes = ['Attack on Titan', 'My Hero Academy', 'One Piece'];

console.log(animes.length); // 3

prototype

prototype allows you to add properties and methods to an array object.

Array.prototype.sumScore = function () {
  return this.reduce((a, b) => a + b);
}

scores = [10, 20, 14, 17, 20];

scores.sumScore();

Note: Be careful not to use arrow function when creating a prototype. This will result to a context error. The value of this inside the prototype functions will be that of the surrounding context.

Array built-in methods

Here are the list of the default methods in JavaScript array.

Method Description
concat() Joins two or more arrays, and returns a copy of the joined arrays
copyWithin() Copies array elements within the array, to and from specified positions
entries() Returns a key/value pair Array Iteration Object
every() Checks if every element in an array pass a test
fill() Fill the elements in an array with a static value
filter() Creates a new array with every element in an array that pass a test
find() Returns the value of the first element in an array that pass a test
findIndex() Returns the index of the first element in an array that pass a test
forEach() Calls a function for each array element
from() Creates an array from an object
includes() Check if an array contains the specified element
indexOf() Search the array for an element and returns its position
isArray() Checks whether an object is an array
join() Joins all elements of an array into a string
keys() Returns a Array Iteration Object, containing the keys of the original array
lastIndexOf() Search the array for an element, starting at the end, and returns its position
map() Creates a new array with the result of calling a function for each array element
pop() Removes the last element of an array, and returns that element
push() Adds new elements to the end of an array, and returns the new length
reduce() Reduce the values of an array to a single value (going left-to-right)
reduceRight() Reduce the values of an array to a single value (going right-to-left)
reverse() Reverses the order of the elements in an array
shift() Removes the first element of an array, and returns that element
slice() Selects a part of an array, and returns the new array
some() Checks if any of the elements in an array pass a test
sort() Sorts the elements of an array
splice() Adds/Removes elements from an array
toString() Converts an array to a string, and returns the result
unshift() Adds new elements to the beginning of an array, and returns the new length
valueOf() Returns the primitive value of an array