1

I just ran across this javascript snippet:

myArray.length--;

What does it do exactly?

1

2 Answers 2

4

This removes the last items in the array.

var myArray = [1, 2, 3];
myArray.length--;
alert(myArray);

The output is:

[1, 2]
Sign up to request clarification or add additional context in comments.

1 Comment

interesting does not know that!
3

Simple experimentation shows that it chops off last element of the array.

> var a = [1, 2, 3];
=> undefined
> a
=> [1, 2, 3]
> a.length--
=> 3
> a
=> [1, 2]

3 Comments

What do you use for an interpreter? I've never seen an interactive javascript console like that.
Chrome's javascript console :-)
Which by the way you get to by pressing F12, then clicking the >= toolbar icon on the very bottom. That and other features in chrome even allow you to experiment and alter the current page. (The other browsers offer some versions of this feature - also try the jsFiddle website)

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.