while(i--) loop in javascript -
i use while loop as:
while (i<some_value)
i saw while(i--) syntax , thought shorter , cooler , tried 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] **// expected result**
but when try...
while((num_arr.length)--) console.log(num_arr); [4, 8, 7, 1] [4, 8, 7] [4, 8] [4] [] // why happening??
is there hidden things need understand use syntax?
arrays’ length
property writable, , cut off elements or add empty slots appropriate when set it.
var items = [1, 2, 3, 4, 5]; items.length = 3; // items [1, 2, 3] items.length = 6; // items sparse array: [1, 2, 3, undefined × 3]
so, don’t that.
Comments
Post a Comment