How do you add an element to the end of an array in JavaScript?
Use the push() method. For example, arr.push(element) adds element to the end of arr.
Click to reveal answer
beginner
What method removes the last element from an array?
The pop() method removes the last element and returns it. It changes the original array.
Click to reveal answer
beginner
How can you add an element to the beginning of an array?
Use the unshift() method. For example, arr.unshift(element) adds element to the start of arr.
Click to reveal answer
intermediate
What does the splice() method do?
It can add, remove, or replace elements in an array at any position. For example, arr.splice(start, deleteCount, item1, item2) removes deleteCount elements from start and inserts new items.
Click to reveal answer
beginner
How do you change the value of an element at a specific index in an array?
Assign a new value directly using the index. For example, arr[2] = newValue changes the third element to newValue.
Click to reveal answer
Which method adds an element to the end of an array?
Apush()
Bpop()
Cshift()
Dunshift()
✗ Incorrect
The push() method adds an element to the end of an array.
What does arr.pop() do?
AAdds an element to the end
BAdds an element to the start
CRemoves the first element
DRemoves the last element
✗ Incorrect
pop() removes and returns the last element of the array.
How do you remove elements from the middle of an array?
AUsing <code>slice()</code>
BUsing <code>splice()</code>
CUsing <code>push()</code>
DUsing <code>pop()</code>
✗ Incorrect
splice() can remove elements from any position in the array.
Which method adds elements to the beginning of an array?
Aunshift()
Bpush()
Cpop()
Dshift()
✗ Incorrect
unshift() adds elements to the start of an array.
How do you change the value of the second element in an array named arr?
Aarr.set(2, newValue)
Barr[2] = newValue
Carr[1] = newValue
Darr.change(1, newValue)
✗ Incorrect
Array indexes start at 0, so the second element is at index 1.
Explain how to add and remove elements from both ends of an array in JavaScript.
Think about methods that work at the start and end of the array.
You got /6 concepts.
Describe how the splice() method can be used to modify an array.
Consider how you can remove and add items anywhere in the array.
You got /6 concepts.
Practice
(1/5)
1. Which method adds one or more elements to the end of a JavaScript array?
easy
A. push()
B. pop()
C. shift()
D. unshift()
Solution
Step 1: Understand array methods for adding elements
push() adds elements to the end, unshift() adds to the start.
Step 2: Identify the method that adds to the end
push() is the method that adds elements at the end of the array.
Final Answer:
push() -> Option A
Quick Check:
Adding at end = push() [OK]
Hint: push() adds at end, unshift() adds at start [OK]
Common Mistakes:
Confusing push() with pop()
Using shift() to add elements
Thinking unshift() adds at end
2. Which of the following is the correct syntax to remove the first element from an array arr?
easy
A. arr.pop()
B. arr.shift()
C. arr.unshift()
D. arr.push()
Solution
Step 1: Recall methods that remove elements
pop() removes from end, shift() removes from start.
Step 2: Identify method to remove first element
shift() removes the first element of the array.
Final Answer:
arr.shift() -> Option B
Quick Check:
Remove first element = shift() [OK]
Hint: shift() removes first, pop() removes last [OK]
splice(1, 2, 8, 9) means start at index 1, remove 2 elements, then insert 8 and 9.
Step 2: Apply splice on array
Original array: [1, 2, 3, 4]. Remove elements at index 1 and 2 (2 and 3), then insert 8 and 9 at index 1. Result: [1, 8, 9, 4].
Final Answer:
[1, 8, 9, 4] -> Option A
Quick Check:
splice removes and inserts = [1, 8, 9, 4] [OK]
Hint: splice(start, deleteCount, items...) edits array in place [OK]
Common Mistakes:
Confusing splice with slice
Not removing correct elements
Forgetting splice inserts new items
4. The following code tries to remove the element '3' from the array but does not work as expected. What is the error?
let arr = [1, 2, 3, 4];
arr.splice(3, 1);
console.log(arr);
medium
A. The index 3 is out of range for removal
B. The splice method is used incorrectly; it should be pop()
C. The index 3 removes the wrong element, should be 2
D. splice cannot remove elements from arrays
Solution
Step 1: Identify the element to remove and its index
The element '3' is at index 2 in the array [1, 2, 3, 4].
Step 2: Check the splice call
The code calls arr.splice(3, 1), which removes the element at index 3 (which is 4), not 3.
Final Answer:
The index 3 removes the wrong element, should be 2 -> Option C
Quick Check:
Remove '3' at index 2, not 3 [OK]
Hint: Check array indexes carefully before splice [OK]
Common Mistakes:
Using wrong index for splice
Confusing pop() with splice()
Thinking splice can't remove elements
5. You have an array arr = [5, 10, 15, 20]. You want to replace the middle two elements with 12 and 18 using a single method call. Which code correctly does this?
hard
A. arr.splice(2, 2, 12, 18);
B. arr.splice(0, 2, 12, 18);
C. arr.splice(1, 1, 12, 18);
D. arr.splice(1, 2, 12, 18);
Solution
Step 1: Identify indexes of middle two elements
Array is [5, 10, 15, 20]. Middle two elements are at indexes 1 and 2 (10 and 15).
Step 2: Use splice to replace these elements
Call arr.splice(1, 2, 12, 18) to start at index 1, remove 2 elements, and insert 12 and 18.