Sam Washburn
Sam Washburn

Reputation: 1827

Decrement array length in Javascript

I just ran across this javascript snippet:

myArray.length--;

What does it do exactly?

Upvotes: 1

Views: 2717

Answers (2)

Luis Perez
Luis Perez

Reputation: 28120

This removes the last items in the array.

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

The output is:

[1, 2]

Upvotes: 4

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230346

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]

Upvotes: 3

Related Questions