Popping and Pushing (more memory tricks)


The push() and pop() methods add elements to and remove elements from the back of an array.

var arr = [1, 2, 3];
console.log(arr); // [ 1, 2, 3, 4 ]
var poppedValue = arr.pop();
console.log(poppedValue); // 4
console.log(arr); // [ 1, 2, 3 ]

Memory Tricks

  • To associate these methods with each other, remember that push-pops are delicious. (I think of them as ice cream treats*, but in my research for this blog post, I discovered that they're actually those weird/gross candy sucker things. Whatever. To me, push-pops = frozen novelties === DELICIOUS)
  • Both of these methods do something to the back of an array, so I like to think of push and pop as a kind of dance method involving your butt. (Like, "Salt-Salt-Salt-N-Pepa's here! ... Want you to push it back!" (and then they do something with their butts) You know that song, right?** #Imightnotbeyoung)
  • I have used push so often that I know it changes the length of an array, but sometimes forget that pop also changes the array -- keeping them associated together will help me remember this.

Keeping these methods straight is pretty intuitive for me, you push something on, and you pop something off. So I don't need a memory trick for that part at least.

* As it turns out, the ice cream treats I'm thinking of are actually called Push-Ups, which sounds like a horribly unpleasant activity to do at all, let alone while eating ice cream.

** Did you know Salt-N-Pepa's "Push It" came out in 1986?!! Also, did you know that video is the best BEST not-actually-a-music-video music video in the history of EVER? I love both Salt and Pepa. I even love the awkwardly capitalized 'N' in between them. Man. If I could, I'd just stop coding right now and watch old Salt-N-Pepa videos ALL DAY.