Another way to change the data in an array is with the .pop() method.
.pop() removes the last element of an array and also returns the value of that element:
let threeArr = [1, 4, 6];
// Remove last element:
let oneDown = threeArr.pop();
console.log(oneDown); // Output: 6
console.log(threeArr); // Output: [1, 4]
// Remove last element again:
let oneDown = threeArr.pop();
console.log(oneDown); // Output: 4
console.log(threeArr); // Output: [1]
Assignment
Follow the Coding Tutorial and let's play with some arrays.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
