Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Removing Items From Arrays

Enemuo Felix
Enemuo Felix
1,895 Points

Removing Items from an Array

Do we still use the 0-index value when removing items from an array with array.shift and array.pop ? I'm suprised Dave used an empty empty parenthesis using the .shift and .pop in the example. How do i remove 0 and 5 here?

var nums = [0, 1, 2, 3, 4, 5];
Enemuo Felix
Enemuo Felix
1,895 Points

Katie Wood Does that mean that 0-index value does not apply when removing items from arrays or with .splice() only? 'cos in your first example you placed 4 at index 5

Oops, that was a mistake on my part - it was at index 4, and I corrected my answer accordingly. Sorry if that caused any confusion! I had just finished an example that used values starting from 1, so I messed myself up.

2 Answers

Salvador Cervantes
Salvador Cervantes
10,898 Points

Hey, I'm sorry to do this to you would be better off reading this. It explains everything for you and will give you a million times better examples and explanation than I ever could. I recommened you open up the console in your web browser and follow along :D

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Salvador Cervantes
Salvador Cervantes
10,898 Points

if you use nums.pop(); this will remove the last item in the array.

if you use nums.shift(); this will remove the first item in the array.

Mark Pryce
Mark Pryce
8,804 Points

Salvador is correct, since shift and pop target the first and last element in an array respectively. Declaring an index value is unnecessary.

Enemuo Felix
Enemuo Felix
1,895 Points

Thanks Salvador, but what about if I want to remove the number 4? Pls, I need someone who can explain to me with examples Jennifer Nordell Steven Parker Katie Wood

Mark Pryce
Mark Pryce
8,804 Points

Then you would splice()

Hey Enemuo,

Mark is correct - to remove a specific value you would use the splice() method. Here are a couple examples of what the syntax looks like for removing the number 4 in the array. The first one is if you know the index of the value already (like we do here), and the other one is if you know you want to remove the number 4, but you don't know the index:

//if you know the index of the value
nums.splice(4);  //4 is the index (number 4 is at index 4 in your example)


//if you don't know the index (if nums were altered by code or generated from another source, etc.)
let indexToRemove = nums.indexOf(4);   //finds the index of 4 in the array
nums.splice(indexToRemove);

The splice() method also has some optional parameters, but those are the basics. You can find more detail here if interested. :)