Assuming I have an array that has a size of N (where N > 0), is there a more efficient way of prepending to the array that would not require O(N
Calling unshift only returns the length of the new array.
So, to add an element in the beginning and to return a new array, I did this:
let newVal = 'someValue';
let array = ['hello', 'world'];
[ newVal ].concat(array);
or simply with spread operator:
[ newVal, ...array ]
This way, the original array remains untouched.