Removing items with pop, shift and splice

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Removing items with pop, shift and splice

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. let a = [1, 2, 3]; a.pop(); What is a now?
A [1, 2]
B [2, 3]
2. let a = [1, 2, 3]; a.shift(); What is a now?
A [2, 3]
B [1, 2]
3. Which call removes 2 items starting at index 1?
A arr.splice(1, 2)
B arr.splice(2, 1)
4. Do pop, shift and splice change the original array?
A No, they return a new array and leave the original alone
B Yes, they change the original array
5. let names = ['Ava', 'Liam', 'Mia']; let x = names.pop(); What is x?
A 'Ava'
B 'Mia'

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1playlist.pop() Alet lastItem = items.pop();
2queue.shift() BRemoves the first person from the queue array and returns them
3Remove 2 items from the scores array, starting at index 1 Cscores.splice(1, 2)
4Remove the last item and save it in a variable called lastItem Dpop() always removes the last item and does not take an index. To remove the first item use fruits.shift() instead.
5fruits.pop(0); Esplice with only an index removes everything from index 2 to the end. To remove just one item, give a count: names.splice(2, 1).
6names.splice(2); FRemoves the last song from the playlist array and returns it

Definitions

pop(), shift(), splice()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let songs = ["Rock", "Pop", "Jazz"]; songs.___(); console.log(songs);
2.
let line = ["Zoe", "Leo", "Isla"]; line._____(); console.log(line);
3.
let nums = [10, 20, 30, 40]; nums.splice(_, 1); console.log(nums); // [10, 20, 40]
4.
let scores = [5, 6, 7, 8, 9]; scores.splice(1, _); console.log(scores); // [5, 8, 9]
5.
let bag = ["pen", "ruler", "book"]; bag.______(1, 1); // removes "ruler" console.log(bag.length); // 2

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let stack = [5, 6, 7];
B stack.push(8);
C stack.pop;
D console.log(stack);
Question 2
A let line = ["A","B","C","D"];
B line.shift();
C line.pop();
D console.log(line.length()); // 2
Question 3
A let nums = [10, 20, 30, 40];
B nums.slice(1, 2);
C console.log(nums);
Question 4
A let pets = ["cat","dog"];
B pets.splice(0, 1);
C console.log(pets[1]); // dog
Question 5
A const bag = ["pen" "ruler"];
B bag.pop();
C console.log(bag);

Answer Key

Removing items with pop, shift and splice
Section A: Quick Questions
Q1
A — [1, 2]
Q2
A — [2, 3]
Q3
A — arr.splice(1, 2)
Q4
B — Yes, they change the original array
Q5
B — 'Mia'
Section B: Match the Pairs
Q1
playlist.pop() → Removes the last song from the playlist array and returns it
Q2
queue.shift() → Removes the first person from the queue array and returns them
Q3
Remove 2 items from the scores array, starting at index 1 → scores.splice(1, 2)
Q4
Remove the last item and save it in a variable called lastItem → let lastItem = items.pop();
Q5
fruits.pop(0); → pop() always removes the last item and does not take an index. To remove the first item use fruits.shift() instead.
Q6
names.splice(2); → splice with only an index removes everything from index 2 to the end. To remove just one item, give a count: names.splice(2, 1).
Section C: Definitions
Q1
pop(), shift(), splice() — Array methods that remove items and change the original array: pop removes the last item, shift removes the first, splice(index, count) removes count items starting at index.
Section D: Fill in the Blanks
Q1
let songs = ["Rock", "Pop", "Jazz"]; songs.pop(); console.log(songs);
Q2
let line = ["Zoe", "Leo", "Isla"]; line.shift(); console.log(line);
Q3
let nums = [10, 20, 30, 40]; nums.splice(2, 1); console.log(nums); // [10, 20, 40]
Q4
let scores = [5, 6, 7, 8, 9]; scores.splice(1, 2); console.log(scores); // [5, 8, 9]
Q5
let bag = ["pen", "ruler", "book"]; bag.splice(1, 1); // removes "ruler" console.log(bag.length); // 2
Section E: Spot the Error
Q1
C — pop is a method, so it needs parentheses to run. Without them the line does nothing and no item is removed. It should be stack.pop();
Q2
D — length is a property, not a method, so it has no parentheses. It should be line.length. After shift and pop, 2 items remain.
Q3
B — slice only copies part of an array and leaves the original unchanged. To remove items from the original array, use nums.splice(1, 2);
Q4
C — splice(0, 1) removed "cat", so only "dog" is left, at index 0. Index 1 is beyond the end and gives undefined. It should be pets[0].
Q5
A — Array items must be separated by commas. A comma is missing between "pen" and "ruler". It should be ["pen", "ruler"].