Is inserting at the beginning of an array efficient? Why or why not?
It is less efficient than adding at the end because all elements must be shifted one position to the right, which takes time proportional to the array size.
Click to reveal answer
Which method adds an element to the start of a JavaScript array?
Aunshift()
Bpush()
Cpop()
Dshift()
✗ Incorrect
unshift() adds elements at the beginning; push() adds at the end.
What happens to the existing elements when you insert at the beginning of an array?
AThey stay in the same place
BThey move one position to the right
CThey move one position to the left
DThey get deleted
✗ Incorrect
Elements shift right to make space for the new element at index 0.
What is the output after this code? let arr = [10, 20]; arr.unshift(5); console.log(arr);
A[10, 5, 20]
B[10, 20, 5]
C[5, 10, 20]
D[20, 10, 5]
✗ Incorrect
unshift(5) adds 5 at the start, so array becomes [5, 10, 20].
Which of these is a downside of inserting at the beginning of an array?
AIt deletes the last element
BIt reverses the array
CIt only works on empty arrays
DIt requires shifting all elements
✗ Incorrect
All elements must move right, which can be slow for large arrays.
If you want to add multiple elements at the beginning, which method can you use?
Aunshift(element1, element2, ...)
Bpush(element1, element2, ...)
Cconcat(element1, element2, ...)
Dslice(element1, element2, ...)
✗ Incorrect
unshift() can take multiple elements to add at the start.
Explain how inserting an element at the beginning of an array works in JavaScript.
Think about what happens to existing elements and the new element's position.
You got /4 concepts.
Describe the performance impact of inserting elements at the beginning of an array compared to the end.
Consider how many elements move when inserting at start vs end.
You got /4 concepts.
Practice
(1/5)
1. What does the unshift() method do in a JavaScript array?
easy
A. Adds one or more elements to the beginning of the array
B. Removes the first element from the array
C. Adds one or more elements to the end of the array
D. Removes the last element from the array
Solution
Step 1: Understand the unshift() method
The unshift() method adds elements to the start of an array, shifting existing elements to the right.
Step 2: Compare with other array methods
push() adds to the end, shift() removes from the start, and pop() removes from the end.
Final Answer:
Adds one or more elements to the beginning of the array -> Option A
Quick Check:
unshift() adds at start = B [OK]
Hint: Remember: unshift = add at front [OK]
Common Mistakes:
Confusing unshift() with push()
Thinking unshift() removes elements
Mixing unshift() with shift()
2. Which of the following is the correct syntax to insert the value 10 at the beginning of array arr?
easy
A. arr.pushFront(10);
B. arr.addFirst(10);
C. arr.unshift(10);
D. arr.insertAt(0, 10);
Solution
Step 1: Recall JavaScript array methods
JavaScript arrays use unshift() to add elements at the start.
Step 2: Check each option
Only arr.unshift(10); is valid JavaScript syntax for this operation.
Final Answer:
arr.unshift(10); -> Option C
Quick Check:
Correct syntax for start insertion = A [OK]
Hint: Use unshift() to add at start, push() for end [OK]
Common Mistakes:
Using non-existent methods like addFirst or pushFront
Confusing unshift() with push()
Trying to use insertAt which is not a JS array method
3. What will be the output of the following code?
let arr = [2, 3, 4];
arr.unshift(1);
console.log(arr);
medium
A. [1, 3, 4]
B. [1, 2, 3, 4]
C. [2, 3, 4, 1]
D. [2, 3, 4]
Solution
Step 1: Understand unshift effect on array
The unshift(1) adds 1 at the start, shifting existing elements right.
Step 2: Trace the array after unshift
Original array: [2, 3, 4] After unshift(1): [1, 2, 3, 4]
Final Answer:
[1, 2, 3, 4] -> Option B
Quick Check:
unshift adds at front = [1, 2, 3, 4] [OK]
Hint: unshift adds element at front, shifts others right [OK]
Common Mistakes:
Thinking unshift adds at end
Ignoring that unshift shifts elements
Expecting original array unchanged
4. Identify the error in the following code that tries to insert 0 at the beginning of array nums:
let nums = [1, 2, 3];
nums.unshift = 0;
console.log(nums);
medium
A. Using assignment instead of calling unshift as a function
B. Array nums is not defined
C. unshift cannot add zero
D. Syntax error due to missing parentheses
Solution
Step 1: Analyze the assignment to unshift property
The code assigns 0 to nums.unshift, overwriting the method instead of calling it.
Step 2: Correct usage of unshift
To add 0 at start, call nums.unshift(0); as a function, not assign a value.
Final Answer:
Using assignment instead of calling unshift as a function -> Option A
Quick Check:
unshift must be called, not assigned = A [OK]
Hint: Call unshift() as function, don't assign it a value [OK]
Common Mistakes:
Assigning value to unshift instead of calling it
Missing parentheses when calling unshift
Thinking unshift is a property, not a method
5. Given the array let data = [5, 6, 7];, which code snippet correctly inserts the numbers 2, 3, 4 at the beginning so that the final array is [2, 3, 4, 5, 6, 7]?
hard
A. data.unshift([2, 3, 4]);
B. data.splice(0, 0, [2, 3, 4]);
C. data.push(2, 3, 4);
D. data.unshift(2, 3, 4);
Solution
Step 1: Understand unshift with multiple arguments
The unshift() method can take multiple arguments and adds them all at the start in order.
Step 2: Analyze each option's effect
data.unshift([2, 3, 4]); adds a single array as first element, resulting in nested array. data.unshift(2, 3, 4); adds 2, 3, 4 individually at start, producing desired array. data.push(2, 3, 4); adds elements at end. data.splice(0, 0, [2, 3, 4]); inserts a single array at index 0, nesting again.
Final Answer:
data.unshift(2, 3, 4); -> Option D
Quick Check:
unshift multiple args adds all at front = D [OK]
Hint: Pass multiple values to unshift() to add all at start [OK]
Common Mistakes:
Passing an array instead of separate values to unshift
Using push() which adds at end
Using splice() incorrectly causing nested arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.