First of all, you should have a clear idea of what arrays are and the purpose of using them in your code. To be precise, arrays are like the containers of your data: they keep the information/data in an ordered form in a similar way to how you may keep your clothes in a drawer.
You can find all the relevant documentation about all the array methods in the MDN docs here.
However, my aim in writing this piece is to address one very common question asked during web developer interviews that is related to the insertion/deletion phenomenon of arrays.
The interviewer may ask, "What array methods can be used to insert/delete in an array, and which is the better ones?
The first part of the question is pretty simple, and you can answer it straight away:
The two methods used for insertion/deletion are push/pop and shift/unshift.
We can illustrate the functionality of these methods as follows:
let fruits = ['Apple', 'Orange', 'Blueberries']
I want to insert one more fruit, but adding the fourth item to the beginning of the array (hence using unshift method) will cause all the elements to re-index, and it will require more effort. However, if we use the push method, then the fourth item will be added straight to the end, and none of the other items will be disturbed. The same goes for the deletion theory; hence, we can conclude that push/pop is better and faster to use than shift/unshift while working on
That’s all folks… Happy coding…
View all Courses