Week #4: Tutorials + JS Arrays + Blog Question.


Problem Set:

 arr-prob.html / arr-prob.js

Blog Questions

  • With the array methods pop() / push() and shift() / unshift(), explain the difference between the returned value of each method and the resulting value of the array? Use an example piece of code with the Enlighter tool to support your description.

pop( ) eliminates the last element from the array and allows us to use it. Once it removes the last item removes, it’ll be returned.

var colors = [ "red", "orange", "yellow"] ;
colors.pop ();
["red", orange"];

//pop () returns the removed element
var color = colors.pop; // orange

push( ) is used to add something at the end of an array.

var colors = ["red", "orange", "yellow"];
colors.push ("green");
// ["red", "orange", "yellow", "green"];

unshift ( ) is used to add to the front of an array.

var colors = ["red", "orange", "yellow"];
colors.unshift ("infrared")
// ["infrared", "red", "orange", "yellow"];

shift ( ) is used to remove to the first item of an array.

var colors = ["red", "orange", "yellow"];
// ["orange", "yellow"]

//shift () also returns the removed element.
var colors =colors.shift (); // orange.

Join the Conversation


  1. You’ve got it right that pop() and shift() return the value of the removed element. But push() and unshift() both return a number which is the new length of the array. All methods affect the original array. In technical terms it is described as affecting the array “in-place.”

Leave a comment

Your email address will not be published. Required fields are marked *