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

JavaScript math constants

How to use canvas context rect method for drawing rectangles

JavaScript canvas introduction

How to target all elements of a given class name in JavaScript

E2020 dynamic imports in JavaScript