5
var arr = [1,2,3,4];

I need to get the last one and then delete it from an array called arr:

var arr = [1,2,3]
1
  • 3
    This question was asked by another user, who then deleted it. Since they don't want it associated with their own account, I re-posted it so the answer is preserved. I made the question CW so I don't gain reputation from it. This is the fairest solution I could think of. Commented Jan 27, 2010 at 14:43

2 Answers 2

10

You want to do exactly what the pop method does:

var arr = [1,2,3,4];
//...
var last = arr.pop(); // returns 4, and arr will contain now [1, 2, 3]
Sign up to request clarification or add additional context in comments.

Comments

0

Can use pop method.

    var arr = [1,2,3,4];

    console.log(arr[arr.length -1]); // get the last element
    
    arr.pop(); // delete the last element
     
         (or)

    //other way for deleting the last element
    console.log(arr.splice(arr.length-1, 1));
    
    console.log(arr); // [1,2,3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.