Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Here’s my code:

var arr = [1,0,2];     

                        

arr.slice(-1); 

Can anyone tell me how to remove the last item from the array? 

1 Answer

0 votes
by (19.7k points)

There is a difference between the splice() and slice() method: 

splice() : It removes adds/removes items from an array. splice() method returns the removed items. It also changes the original array which reflects the change(removed item in your case). 

Use this code snippet:  splice(startPosition, deleteCount)

array.splice(-1,1)

Note: if you don’t specify the delete count, it’ll remove the element till the end of the array. 

Slice() :  it returns the selected elements in an array. it doesn’t change the original array so the change (removed item in your case) will not be reflected.

If you want to learn more about Java, then go through this Java tutorial by Intellipaat for more insights.

  

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
0 answers
asked Apr 28, 2021 in Selenium by natalyserga (120 points)
0 votes
1 answer

Browse Categories

...