let arr: number[] = [3, 4, 5]; arr.unshift(2); arr.unshift(1); console.log(arr);
The unshift method adds elements to the beginning of the array. First, 2 is added before [3,4,5], making [2,3,4,5]. Then 1 is added before that, resulting in [1,2,3,4,5].
const arr: string[] = ['c', 'd']; arr.unshift('b'); arr.unshift('a'); console.log(arr.join(' -> '));
Each unshift adds the element to the front. So 'b' is added before ['c','d'], then 'a' before that, resulting in ['a','b','c','d'].
let numbers = [1, 2, 3]; numbers.push(0); console.log(numbers);
push adds elements to the end of the array. To add at the beginning, unshift should be used.
When inserting at the beginning, all existing elements must shift one position to the right, so the time grows linearly with the array size.
Using concat creates a new array with the new elements at the front without shifting existing elements multiple times, which is more efficient for large arrays.
