J O S E P H J O S E P H

JavaScript Arrays

Return a single element

.at()

Returns the element at the index passed to it. Negative integers count back from the last element in the array.

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
arr.at(-1); // 'Winston'

.find()

Returns the first element that satisfies the provided predicate function (or undefined).

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston', 'Winnie'];
arr.find((el) => el.startsWith('W')); // 'Winston'

Return a new array

.map()

Returns a new array with the results of calling a provided function on every element in the array.

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
arr.map((el) => el.toLowerCase()); // ['nick', 'jess', 'schmidt', 'winston']

.filter()

Returns a new array with all elements that satisfy the provided predicate function.

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
arr.filter((el) => el.length > 4); // ['Schmidt', 'Winston']

.concat()

Merges two or more arrays or values into a single array and returns it.

const arr1 = ['Nick', 'Jess', 'Schmidt', 'Winston'];
const arr2 = ['Cici', 'Coach'];
const str = 'Robby';
arr1.concat(arr2, str); // ['Nick', 'Jess', 'Schmidt', 'Winston', 'Cici', 'Coach', 'Robby']

...

The spread operator can also be used to merge arrays.

const arr1 = ['Nick', 'Jess', 'Schmidt', 'Winston'];
const arr2 = ['Cici', 'Coach'];
[...arr1, ...arr2]; // ['Nick', 'Jess', 'Schmidt', 'Winston', 'Cici', 'Coach']

Return a string

.join()

Concatenates all elements in the array into a single string, separated by commas or by a provided string.

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
arr.join(); // 'Nick,Jess,Schmidt,Winston'
arr.join('-'); // 'Nick-Jess-Schmidt-Winston'

Return a boolean

.includes()

Returns true if the provided element is present in the array.

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
arr.includes('Winston'); // true

.some()

Returns true if at least one element in the array passes a provided predicate function.

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
arr.some((el) => el.startsWith('J')); // true

.every()

Returns true if every element in the array passes a provided predicate function.

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
arr.every((el) => typeof el === 'string'); // true

No return

.forEach()

Calls a provided function once for each element in the array.

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
arr.forEach((el) => console.log(el));
// Nick
// Jess
// Schmidt
// Winston

// With index argument in the callback:
arr.forEach((el, index) => console.log(`${el} at index ${index}`));
// Nick at index 0
// Jess at index 1
// Schmidt at index 2
// Winston at index 3

for...of

Loops over elements in the array, performing some operation on each element (usually calling a function).

const arr = ['Nick', 'Jess', 'Schmidt', 'Winston'];
for (const el of arr) {
  console.log(el);
}
// Nick
// Jess
// Schmidt
// Winston

// With destructuring:
for (const [index, el] of arr.entries()) {
  console.log(`${el} at index ${index}`);
}
// Nick at index 0
// Jess at index 1
// Schmidt at index 2
// Winston at index 3