1

I normally use while loop as:

while (i<some_value) 

I saw while(i--) syntax and thought it is shorter and cooler and tried the following in google-chrome.

var num_arr= [4,8,7,1,3];


var length_of_num_arr=num_arr.length;


while(length_of_num_arr--) console.log(num_arr);
 [4, 8, 7, 1, 3]
 [4, 8, 7, 1, 3]
 [4, 8, 7, 1, 3]
 [4, 8, 7, 1, 3]
 [4, 8, 7, 1, 3] **// THIS IS EXPECTED RESULT**

But When I try...

while((num_arr.length)--) console.log(num_arr);
[4, 8, 7, 1]
[4, 8, 7]
[4, 8]
[4]
[] // WHY IS THIS HAPPENING??

Is there some hidden things you need to understand to use this syntax?

4
  • num_arr.length-- is the same as num_arr.length = num_arr.length - 1. Is that clearer now? Commented Jun 19, 2015 at 2:28
  • Never thought array length was writable! Thanks Commented Jun 19, 2015 at 2:29
  • @Jack_of_All_Trades: Although, if it wasn’t, this still wouldn’t work – the trick depends on length_of_num_arr being decremented, after all. Commented Jun 19, 2015 at 2:31
  • @minitech: yes you are right....my brain working bad.... Thanks to all of you guys... Commented Jun 19, 2015 at 2:33

3 Answers 3

8

Arrays’ length property is writable, and will cut off their elements or add empty slots as appropriate when you set it.

var items = [1, 2, 3, 4, 5];
items.length = 3; // items is now [1, 2, 3]
items.length = 6; // items is now a sparse array: [1, 2, 3, undefined × 3]

So, don’t do that.

Sign up to request clarification or add additional context in comments.

7 Comments

Wow!....One thing I knew today at 10:28 PM ..Javascript you never fail to surprise me..
Is it one of the thing that only happens in javascript? I don't think you can change the length of array this way in any other languages....I don't remember at least in python....
@Jack_of_All_Trades: There’s probably something else out there, but I’m not aware of a writable length property in any other common languages, no. VB.NET has ReDim, Python has del arr[-5:], etc. as far as unusual slightly unusual possible resizing methods go.
Note setting length to a higher value does not append undefineds.
That should be: items.length = 6; // items is now a sparse array: [1, 2, 3,,,,];. The additional elements aren't undefined, they don't exist (i.e. they are not defined, rather than exist and have the value undefined).
|
2

When you do array.length-- you're potentially shortening the array by one element each time.

See the reference under the section Shortening the array from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length

Array.prototype.length can be re-written programmatically and it potentially shorten your array by the new length you assign.

For example

a = [1,2,3,4,5,6,7,8,9,10];

// Shorten the array by one element
a.length--; // a <-- [1,2,3,4,5,6,7,8,9]

// In case you want to shorten your array to 3 elements, you can:
a.length = 3; // a <-- [1,2,3]

Comments

1

When you set the length property of an array to a lower value, the items at the end are removed:

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

This is described in the spec:

While newLen < oldLen repeat,

  1. Set oldLen to oldLen – 1.
  2. Let deleteSucceeded be the result of calling the [[Delete]] internal method of A passing ToString(oldLen) and false as arguments.

In your code you use the postfix decrement operator (--) which reduces the length of the array.

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.