Arrays – Mutating Methods

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

Arrays – Mutating Methods

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does push() add an element to the end of an array?
A Yes
B No
2. Does pop() remove the first element of an array?
A No
B Yes
3. Does unshift() add an element to the start of an array?
A No
B Yes
4. Does shift() remove the first element of an array?
A No
B Yes
5. Do these methods change the original array?
A Yes
B No

Match the Pairs

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

Left columnRight column
1nums.push(4) Anums.unshift(1)
2nums.pop() Bnums.shift()
3Adds an element to the start of an array CAdds 4 to the end of the nums array
4Removes the first element from an array DWrong - use pop(), not remove(), to remove the last element
5a.add(3); EWrong - use push(), not add(), to add to the end
6b.remove(); FRemoves the last element from the nums array

Definitions

push()

Fill in the Blanks

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

1.
nums.____(4);
2.
nums.___();
3.
nums._______(1);
4.
let first = nums._____();
5.
scores.____(90);

Spot the Error

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

Question 1
A a.add(3);
B let a = [1, 2];
C console.log(a);
Question 2
A let b = [1, 2, 3];
B b.remove();
C console.log(b);
Question 3
A let c = [2, 3];
B console.log(c);
C c.push;
Question 4
A let d = [1, 2, 3];
B console.log(d);
C let e2 = 5;
D d.Pop();
Question 5
A let e = [2, 3];
B console.log(e);
C let f2 = 1;
D console.log(f2);
E e.Push(1);

Answer Key

Arrays – Mutating Methods
Section A: Quick Questions
Q1
A — Yes
Q2
A — No
Q3
B — Yes
Q4
B — Yes
Q5
A — Yes
Section B: Match the Pairs
Q1
nums.push(4) → Adds 4 to the end of the nums array
Q2
nums.pop() → Removes the last element from the nums array
Q3
Adds an element to the start of an array → nums.unshift(1)
Q4
Removes the first element from an array → nums.shift()
Q5
a.add(3); → Wrong - use push(), not add(), to add to the end
Q6
b.remove(); → Wrong - use pop(), not remove(), to remove the last element
Section C: Definitions
Q1
push() — Adds one or more elements to the end of an array, changing the original array.
Section D: Fill in the Blanks
Q1
nums.push(4);
Q2
nums.pop();
Q3
nums.unshift(1);
Q4
let first = nums.shift();
Q5
scores.push(90);
Section E: Spot the Error
Q1
A — Use push(), not add(), to add to the end of an array.
Q2
B — Use pop(), not remove(), to remove the last element.
Q3
C — push() needs parentheses to actually call the method.
Q4
D — 'pop' must be lowercase - a capital P causes an error.
Q5
E — 'push' must be lowercase - a capital P causes an error.