Handy array functions every developer should know JavaScript

Array make a good means of data storage and manipulation in JavaScript

if used well arrays are way better than objects especially if you're working with numbers

Array.push(element)
Its a method used for quickly appending an element to an array
For instance
const arr = ['code']
arr.push('eat')
// ['eat', 'code']

Array.unshift(element)
It's a method that's pretty similar to array.push except that unshift prepends an element to the end of the array
For instance
const arr = ['eat', 'code']
arr.unshift('sleep')
// ['eat', 'code', 'sleep']

Array.shift()
Array.shift is a function that removes the first element of an array and returns it
For instance
const arr = ['eat', 'code']
const err = arr.shift()
arr returns
// ['sleep']
err returns
// eat

Array.pop()
Is a method an array property similar to shift that removes the last element of an array and returns it.

Array.pop() and array.shift() both modify their parent array they are destructive

For instance
const arr = ['eat', 'code']
const err = arr.pop()
arr returns
// ['eat']
err returns
// code

Comments

Popular posts from this blog

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript