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

Open-graph meta data tags introdution

What is 'this.' keyword in JavaScript

How to create zip archives in nodejs

what is 'array.includes' function in JavaScript

How to build a twitter retweet bot using JavaScript