First of all, you should have a very clear idea about what arrays are and what is 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 just like you keep your clothes in a cupboard in an organized manner:
However, my aim of writing this piece is to address one very common question asked during web developer interviews and it is related to the insertion/deletion phenomenon of arrays.
The interviewer may ask which are the array methods used for inserting/deleting in an array and which are the better ones?
The first part of the question is pretty simple and you can answer it straightaway...The two methods used for insertion/deletion are push/pop and shift/unshift.
We can illustrate the functionality of these methods as follows:
However, the second part of the question is little tricky..how can you judge which is one is better and which is not..we can answer this question in terms of number of operations, the one which takes lesser operations will be better. For example we have the following array:
let fruits = ['Apple', 'Orange', 'Blueberries']
and I want to insert one more fruit to this array, adding the fourth item to the beginning of the array (hence using unshift method) will cause all the elements to re-index and hence more effort required however if we used the push method then the fourth item will be added straight to the end and none other items will be disturbed, same goes for the deletion theory hence we conclude that push/pop is better and faster than shift/unshift while working on really large arrays having thousands and thousands of records.
That's all folks..