Working with arrays in JavaScript (a usefull content)

Leandro A. Siqueira
2 min readMay 16, 2024

--

The Daily Life Of Darth Vader by Paweł Kadysz

Array.push()

Adds elements to the end of an array.

let Array = ['banana', 'maçã', 'laranja']; 
Array.push('morango');

Array now is [‘banana’, ‘maçã’, ‘laranja’, ‘morango’]

Array.pop()

Removes the last element from an array and returns it.

let Array = ['banana', 'maçã', 'laranja']; 
let lastItem = Array.pop();

lastItem is ‘laranja’,

Array now is [‘banana’, ‘maçã’]

Array.shift()

Removes the first element from an array and returns it.

let Array = ['banana', 'maçã', 'laranja']; 
let RemovedArray = Array.shift();

RemovedArray is ‘banana’, frutas now is [‘maçã’, ‘laranja’]

Array.unshift()

Adds elements to the beginning of an array.

let Array = ['banana', 'maçã', 'laranja']; 
Array.unshift('morango');

Array now is [‘morango’, ‘banana’, ‘maçã’, ‘laranja’]

Array.splice()

Alters the content of an array by removing or replacing elements.

let Array = ['banana', 'maçã', 'laranja', 'morango']; 
Array.splice(2, 1, 'uva');

Array now is [‘banana’, ‘maçã’, ‘uva’, ‘morango’]

Array.concat()

Combines two or more arrays.

let Array = ['banana', 'maçã']; 
let CitrusArray = ['laranja', 'limão'];
let AllFruits = Array.concat(CitrusArray);

AllFruits is [‘banana’, ‘maçã’, ‘laranja’, ‘limão’]

slice()

Returns a copy of a portion of an array.

let Array = ['banana', 'maçã', 'laranja', 'morango']; 
let SomeFruits = Array.slice(1, 3);

SomeFruits is [‘maçã’, ‘laranja’]

Array.forEach()

Executes a function for each element in the array.

let Array = ['banana', 'maçã', 'laranja']; 
Array.forEach((fruta) => { console.log(fruta); });

Output: banana, maçã, laranja

Array. map()

Creates a new array with the results of a function for each element in the original array.

let Array = [1, 2, 3]; 
let DuplicatedArray = Array.map(function(number) { return number * 2; });

DuplicatedArray is [2, 4, 6]

Array. filter()

Creates a new array with all elements that pass a specified test provided by a function.

let Array = [1, 2, 3, 4, 5]; 
let PairArray = Array.filter(function(number) { return number % 2 === 0; });

PairArray is [2, 4]

Array.find()

Returns the first element in an array that satisfies a condition provided by a function.

let Array = [10, 20, 30, 40, 50]; 
let GreaterArray = Array.find(function(number) { return number > 25; });

GreaterArray is 30

Array.reduce()

Executes a function for each element in the array, resulting in a single value.

let Array = [1, 2, 3, 4, 5]; 
let SumArray = Array.reduce((accumulator, value) => accumulator + value, 0);

Sum is 15

Array.indexOf()

Returns the first index of the specified element in an array.

let Array = ['banana', 'maçã', 'laranja']; 
let Index = Array.indexOf('laranja');

Index is 2

Array.sort()

Sorts the elements of an array.

let Array = [3, 1, 4, 2, 5]; 
Array.sort();

Array now is [1, 2, 3, 4, 5]

Array.includes()

Checks if an array includes a certain element.

let Array = ['banana', 'maçã', 'laranja']; 
let TeamBanana = Array.includes('banana');

TeamBanana is true

I used Array as a common name, you can name your arrays related to the content or target.

--

--

Leandro A. Siqueira
Leandro A. Siqueira

Written by Leandro A. Siqueira

Well, I woke up this morning and I got myself a beer!

No responses yet