FAQ: Lists - Adding Items to a List

Hello, @tcart38.

Welcome to the forums.

At this point, it isn’t really important to understand exactly how the Array.splice() method works, but since you asked, I’ll try to explain.

First of all, Array.splice() is a JavaScript method. You can read about it in the official MDN Docs, and even practice with it yourself here.

This method takes one required argument, and can also take additional optional arguments. Arguments are the values separated by commas between the ( ). The first argument in the example is 1. That is the index where we wish to make a change to the array (referred to as list in the exercise). The second argument is 0. This is the number of array elements that we want to remove. Since we don’t want to remove any, the value is 0. The third argument is 'mango', so 'mango' will be added to the array at index 1. If we change these values, we can change the array differently.

const myList = ['apple', 'banana', 'pear']; //const is a way to declare a variable in JavaScript
myList.splice(1, 0, 'mango') //adds mango at index 1, and shifts the remaining elements to the right
//I can print the array using console.log() in JavaScript
console.log(myList);

Output:

[ ‘apple’, ‘mango’, ‘banana’, ‘pear’ ]

If I wanted to delete a couple of the original elements, and add new ones, I could do this:

const myList = ['apple', 'banana', 'pear'];
myList.splice(1, 2, 'mango', 'plum') //starts at index 1, deletes 2 elements, adds mango & plum
console.log(myList);

Output:

[ ‘apple’, ‘mango’, ‘plum’ ]

We could also remove only one element, and add several if we wish:

const myList = ['apple', 'banana', 'pear'];
myList.splice(1, 1, 'mango', 'plum', 'orange', 'grape', 'cherry') //starts at index 1, deletes 1 element, adds mango, plum, orange, grape & cherry
console.log(myList);

Output:

[ ‘apple’, ‘mango’, ‘plum’, ‘orange’, ‘grape’, ‘cherry’, ‘pear’ ]

Notice 'pear' was shifted to the end of the array. We started at index 1, deleted 1 element, and inserted 5 new elements.

Happy coding!

18 Likes

That helps! Thank you so much!

1 Like

Hello
It’s (1, 0’ ‘mango’) because 1 is position of ‘mango’ = index 1
0 is start with index 0 = ’ apple’
and ‘mango’ is the name of fruit

1 Like

i was a little confused on what elements it actually deleted, this explained it perfectly! thank you!

1 Like

Thank you so much for the lovely answer, I mean Codecademy is so awesome.

2 Likes

Every solution you gave makes so much sense. Thank you for the wonderful insights

This is the best explanation. Thank you.

const months = [‘Jan’, ‘March’, ‘April’, ‘June’];
months.splice(1, 0, ‘Feb’);
// inserts at index 1
console.log(months);
// expected output: Array [“Jan”, “Feb”, “March”, “April”, “June”]

months.splice(4, 1, ‘May’);
// replaces 1 element at index 4
console.log(months);
// expected output: Array [“Jan”, “Feb”, “March”, “April”, “May”]

2 Likes

Deletion count can be anything from 0 to how ever many elements start from the insertion point.

1 Like

thank you. I got the logic now.

1 Like

as we know we can give deletion count but I had a question. How will the computer understand which item(s) is about be deleted . for example :
myLIst = [‘apple’,‘banana’.‘pear’,mango’,orange’]
now we want to delete 2 items , pear and mango
so how will the computer understand it is pear and mango?
and how will we do that?
to make it easier just understand we are deleting pear, the same question with that

It will understand by the index we give as an insertion point.

          index  delete_count
             \    /
myList.splice(2, 2, 'lemon')

The above shows index 2 as the insertion point (before)s it shows that two items will be removed, and the lemon item will be inserted in their place.

In relation to this example

myList = [‘apple’, ‘banana’, ‘pear’]
myList.append(‘orange’)
// now, myList == [‘apple’, ‘banana’, ‘pear’, ‘orange’]

Should it be changed to myList.push(‘orange’) to minimize confusion?
I’m only new to javascript, so i’m not sure if append would work in a javascript array under different circumstances. (didn’t work for me in the console)

Welcome to the forums, @vokonick. We can only use .append() on DOM objects, not arrays. For that we need to use Array.push() (to add to the right) or Array.unshift() (to add to the left).

Thanks @mtf yeah, i know. I was just wondering if the educational material for this page should be updated to reflect this. It just might be a bit confusing to newcomers, that’s all :slight_smile:

1 Like

@mtf all good, I did a bit of digging .append is what you would use in python to add to the end of list instead of .push to add to the end of an array in javascript.

1 Like

Pls, can I get cleared on this?
image

:point_up_2: in the pic above we see our list get first assigned to values with a single equal sign(=)

myList=[ 'apple ,' 'banana ,' 'pear' ]

Then right below the myList.append( 'orange' )
We see this
// now, myList == [ 'apple', 'banana', 'pear', 'orange' ]

My question is why does the 1st line of code use a single equal sign(=) and the last a double question mark(=), isn’t a single (=) meant for assignment of variable and double (=) for evaluating to boolean values?

Thanks in advance :slightly_smiling_face:

That line is a comment (albeit not Python), not code. The author is showing what the list will look like by way of a comparison.

>>> myList = ['apple', 'banana', 'pear']
>>> myList.append('orange')
>>> myList == ['apple', 'banana', 'pear', 'orange']
True
>>> 
1 Like

Super uber duber baby coder wannabe here. Just reacting to my instinctual wheels turning…

So is this consistent with the idea that I could essentially rewrite @tag0256722813 first piece of code to say:
const months = [‘Jan’, ‘March’, ‘April’, ‘June’];
months.splice**(1, 3, ‘Feb’);**
// inserts at index 1
console.log(months);
// expected output: Array [“Jan”, “Feb”]

?? I guess the question would be something like, can that ‘0’ that caused so much confusion in the original code from Codecademy be changed to any number as long as there are as many elements(??) in the array?

Hope my butchered layman’s terms make sense. Thank you everyone!

That number can be any positive number (or zero). If the deletion count is greater than the number of elements remaining then all the remaining elements are removed, anyway. It will not throw an error.

myList.splice(1, 0, 'mango')

What do the 1 and 0 mean?