Javascript is a mandatory skill to learn and develop web applications. The strongest building block is arrays. Arrays are seen in different programming languages and are used for storing data. Our previous article taught us the Top 10 javascript array methods. Here are the remaining 7 JavaScript Array Methods You Should Know.
Fill ( ) :
In Javascript, the array fill method fills all the elements inside an array with a provided static value. The fill method changes only the array that is called upon, and also it returns the modified array.
Example :
const numbers = [1,2,3,4,5]
numbers.fill(1)
console.log(numbers)
Output:
[ 1, 1, 1, 1, 1 ]
We can also able to specify the start and end index; let's see with an example that we need to use the fill( ) method from the first index to the fourth index to implement it; the index 1,2,3 are changed, but not the index o, and four has changed.
const numbers = [1,2,3,4,5]
numbers.fill(0,1,4)
console.log(numbers)
Output:
[ 1, 0, 0, 0, 5 ]
Push ( )
In the push method ( ), we can push elements to the array; it need not be a single element. In push(), we can add one or more elements to the end of a collection, which returns the array's new length.
const names = ['harleen','shifali', 'mithali']
Names. push('taniya', 'pooja','sneh rana')
console.log(names)
Output :
[ 'harleen', 'shifali', 'mithali', 'taniya', 'pooja', 'snehrana' ]
Pop ( ) :
In javascript, the pop( ) removes the last element of an array and returns that element in an array.
const names = ['harleen','shifali', 'mithali']
names. pop()
console.log(names)
Output :
[ 'harleen', 'shifali' ]
It also can return the last element in the array. In this case, mithali is the last element.
const names = ['harleen','shifali', 'mithali']
const lastItem = names. pop()
console.log(lastItem)
Output:
mithali
Shift ( )
In Javascript, shift( ) removes the first element of an array and returns that element; it's straight opposite to the pop method.
const numbers = [ 1,2,3]
numbers.shift( )
console.log(numbers)
OUTPUT:
[ 2, 3 ]
Unshift( )
In javascript, the unshift () method helps add new elements to the beginning of an array and returns the new length, which is the opposite of the push method.
Const numbers = [0,1,2,3,4,5]
numbers.unshift(0)
numbers.unshift(-1)
console.log(numbers)
Output:
[ -1, 0, 1, 2, 3, 4, 5]
slice( )
In Javascript, the slice () method selects a part of an array and returns the new collection, and we can choose an array's beginning and end index.
const numbers = [1,2,3,4,5]
Const numbers2 = numbers.slice (1,3)
console.log(numbers2)
Output:
[ 2, 3 ]
forEach( )
In javascript, the array forEach ( ) method executes a given function on every element shown in an array.
Const numbers = [1,2,3,4,5]
numbers.forEach(consoleItem)
Function consoleItem ( item, index, arr) {
console.log(Item)
}
Output:
1
2
3
4
5
Conclusion :
As we discussed, those mentioned above are some of the essential JavaScript Array Methods that a developer should be aware of.
Also, read our article: https://blog.skillsafari.in/top-10-javascript-array-methods-you-should-know