OneCompiler

3 Useful JavaScript Array methods

135


In this post, we will see some useful JavaScript array methods.

1. some

This method checks for a condition in the entire array and returns true if at least one element satisfies the condition.

a = [1,2,3,4]

b = a.some(t => t%2 === 0)

console.log(b)

This outputs,

true

2. every

This method is similar to the same method but it returns true only if all the elements satisfy the condition.

a = [1,2,3,4]

b = a.every(t => t%2 === 0)

console.log(b)

This outputs,

false

3. fill

This method fills particular values in all specified all indices.

a = [1,2,3,4]

a.fill(0,1,3)

console.log(a)

This will output

[ 1, 0, 0, 4 ]