This question has already been answered:

If I create an array in such a way a=[1,2,12] , then I can run through the indices like for (var i in a) , but if I create an array using new Array , then the situation changes:

 b=new Array(10); for(var i in b) console.log(i); 

forin stops running on array indices. What's wrong?

 "use strict"; document.body.innerHTML = "for a: "; var a = [1, 2, 12]; for (var i in a) document.body.innerHTML += `${i} `; document.body.innerHTML += "<br>for b:"; var b = new Array(10); for (var i in b) document.body.innerHTML += `${i} `; 

UPD I will change the question a bit. You can create an array in two ways, get an equivalent result, but at the same time forin will work for these array in different ways. I will show it by example:

 "use strict" var p=(s)=>document.body.innerHTML+=s; var x = [undefined, undefined, undefined]; var y = new Array(3); p(`${x.length} ${y.length}<br>`); for (var i=0; i<3; i++) p(`${x[i] === y[i]} `); p("<br>"); p("forin for x:"); for (var i in x) document.body.innerHTML += `${i} `; p("<br>forin for y:"); for (var i in y) document.body.innerHTML += `${i} `; 

It turns out in something all the same, these arrays are different. In what?

The console in chrome displays them in different ways.

enter image description here

Reported as a duplicate by Qwertiy participants , Grundy , VenZell , Streletz , aleksandr barakin 27 May '16 at 17:07 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 2
    probably array b empty because - Herrgott
  • alert(b) shows ,,,,,,,,,, - Herrgott
  • one
    It seems the only correct answer here should be: never go through the forin array - Alexey Ten

3 answers 3

 b=new Array(10); 

This line creates 10 empty elements. So do not want. Here you can create several elements b=new Array(10, 1, 6, 8) ; So it will work out correctly. or if you already want to create one element, then it is better to do so

 var b = new Array(); b[0] = 10; 
  • I create an array with 10 elements. - pank
  • @SergeyPestov duck it is empty, memory is allocated, but there is no data, what should it output according to yours? air? - Vasily Barbashev
  • 2
    @SergeyPestov look, you updated the question, and my previous comment has already answered it) The place is allocated, but there is no data in it, undefined x N means that you have N undefined values ​​in the array. And in the case of X you assign the value of undefined array elements, which is already a value, thereby you initialize the values ​​by array indices - Vasily Barbashev
  • one
    @SergeyPestov No, they do not match) In X number of arrays not declared by x3, and in Y declared as undefined . It is just necessary to know, in practice (if you write correctly) this will not happen for you. Yes, and allocate memory for N number of elements is almost never needed, everything is done dynamically. This will have to be recognized as a fact) - Vasily Barbashev
  • one
    @SergeyPestov well, you will not have this in practice, and you will declare arrays via new Array() :)) - Vasily Barbashev
 var b = new Array(10); b[0] = "one" b[1] = "two" b[2] = "three" for (var i in b) document.body.innerHTML += `${i} `; 

Выхлоп

 for b: 0 1 2 

UPD

Most likely due to the fact that you initialize array x as [undefined, undefined, undefined] , if you replace it with [,,,] (3 commas, please pay attention), then the exhaust is identical

 3 3 true true true forin for x: forin for y: 
  • Do not check hasOwnProperty bad :) - Vasily Barbashev
  • @ Vasily Barbashev, thank you, I did not know such a function - Herrgott 1:59 pm
  • @ Vasily Barbashev, in most cases, normal, hasOwnProperty is a rather slow function to sniff it in all cycles - Grundy
  • @Grundy everything is relative))) - Vasily Barbashev
  • @Grundy, not normal. Especially when IE8 + es5 polyfiles. - Qwertiy

Because there is nothing in the array. So there is nothing to sort through.

What is the danger of using for .. in for an object or an array?

It turns out that the arrays are different in some way. In what?

In the first there are 3 elements with the value undefined , and in the second there is a void.