Save for later
8 Must-Know
JavaScript Array
Methods
@nacercodes
001
.forEach()
Executes a provided function once for each array
element.
const emojis = [' ', ' ', ' ']
emojis.forEach(emoji => {
console.log(`I love ${emoji}`)
})
Console
I love
I love
I love
@nacercodes
002
.map()
Creates a new array populated with the results of
calling a provided function on every element.
const emojis = [' ', ' ', ' ']
const result = emojis.map(emoji => {
return emoji + emoji
})
console.log(result)
Console
[ ' ', ' ', ' ' ]
@nacercodes
003
.filter()
Creates a new array with all elements that pass the
test implemented by the provided function.
const emojis = [' ', ' ', ' ', ' ']
const pet = emojis.filter(emoji => {
return emoji == ' ' || emoji == ' '
})
console.log(pet)
Console
[ ' ', ' ' ]
@nacercodes
004
.concat()
Creates a new array by merging two or more arrays.
const fruits = [' ', ' ']
const vegetables = [' ', ' ']
const result = fruits.concat(vegetables)
console.log(result)
Console
[ ' ', ' ', ' ', ' ' ]
@nacercodes
005
.find()
Returns the value of the first array element that
satisfies the provided test function, or undefined if
none does.
const emojis = [' ', ' ', ' ', ' ']
const moon = emojis.find(emoji => {
return emoji == ' '
})
console.log(moon)
Console
@nacercodes
006
.push()
Adds the specified elements to the end of an array.
const emojis = [' ', ' ', ' ']
emojis.push(' ')
console.log(emojis)
Console
[ ' ', ' ', ' ', ' ' ]
To add them to the beginning, use the .unshift()
method.
@nacercodes
007
.pop()
Removes the last element from an array and returns it.
const emojis = [' ', ' ', ' ']
const lastEmoji = emojis.pop()
console.log(lastEmoji)
console.log(emojis)
Console
[ ' ', ' ' ]
To remove the first one, use the .shift() method.
@nacercodes
008
.includes()
Determines whether an array includes a certain value
or not.
const emojis = [' ', ' ', ' ']
const hasSun = emojis.includes(' ')
console.log(hasSun)
const hasStar = emojis.includes(' ️')
console.log(hasStar)
Console
true
false
@nacercodes
Nacer Codes
@nacercodes
Save it or lose it. ️
s
de
rco
ce
na