1

I am learning JavaScript and now I am trying to run the following codes in Node to display the values in an array:

let array = [1, 2, 3];
for (let a in array) {
    console.log(a);
}

I expect the above codes will print 1, 2 and 3

However, it prints 0, 1, 2.

Can anyone tell me what's going wrong in my code?

Kelvin Ho
  • 376
  • 2
  • 3
  • 14
  • 1
    `console.log(array[a])`. `a` is the increment. I don't recommend using a for in loop on an Array though. Use `array.forEach(function(value, increment){})`, or just a regular `for` loop if you want to break out of it, for faster execution. In the `forEach` loop `value` will have your value without needing array[increment]. – StackSlave Jul 15 '19 at 00:33
  • I can get the correct output now. So a is actually the index of the array. – Kelvin Ho Jul 15 '19 at 00:38
  • Replace the `in` with `of`. `in` is for iterating over keys of an object, `of` is for iterating over values of an interable. – Ry- Jul 15 '19 at 00:39
  • This is a very good question for beginners to learn the answer to, good job! –  Jul 15 '19 at 00:55

5 Answers5

1

You should never use for/in for iterating an array. And, when you were doing so, you were just getting the array indices, not the actual array elements.

for/in iterates all enumerable properties of the object, not just array elements which can end up including things you do not want. See for ... in loop with string array outputs indices and For loop for HTMLCollection elements for details.

Instead, you can use any number of other options. The modern way is probably for/of which works for any iterable (which includes an array).

for/of

let array = [1, 2, 3];
for (let a of array) {
    console.log(a);
}

.forEach()

let array = [1, 2, 3];
array.forEach(function(a, index) {
    console.log(a);
});

Plain for loop

let array = [1, 2, 3];
for (let i = 0; i < array.length; i++) {
    console.log(array[i]);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

TL;DR: The for..in syntax grabs the properties of the object you iterate over, and not the values of the objects like you expect. The Mozilla Developer Network is a great resource and it is described here.

The properties of the array you are grabbing are the indices, to get the value you need to uses the property of the indices on the array using the array index operator [] like so.

let array = [1, 2, 3];
for (let a in array) {
    console.log(array[a]);
}

Or use the for..of syntax to get the value of the object like so.

let array = [1, 2, 3];
for (let a of array) {
    console.log(a);
}
-1
let array = [1, 2, 3];
for (let a in array) {
    console.log(array[a]);
}
Programnik
  • 1,449
  • 1
  • 9
  • 13
  • 1
    You should NEVER use `for/in` to iterate an array because that iterates all enumerable properties, not just array elements. Use `for/of` or a `.forEach()` or a regular `for` loop. – jfriend00 Jul 15 '19 at 00:39
-1

let array = [1, 2, 3];
for (let a in array) {
    console.log("index:  " + a + ', ' + "value:  " + array[a]);

}

Those are the array indices, starting at 0. If you want the values, you need to get the value at that index.

SScotti
  • 2,158
  • 4
  • 23
  • 41
  • You should NEVER use `for/in` to iterate an array because that iterates all enumerable properties, not just array elements. Use `for/of` or a `.forEach()` or a regular `for` loop. – jfriend00 Jul 15 '19 at 00:45
  • Just curious about this `for (const [key, value] of Object.entries(array)) { console.log("index: " + key + ": " + "value: " + value); }`. That can be used for objects and arrays ? When would you use that ? – SScotti Jul 15 '19 at 00:57
  • I don't think I would ever use that for an array because that will also include enumerable properties, not just array elements. Works well for iterating the properties of an object, though. If I wanted both index and item for an array, I'd either use `.forEach()` or `for (let [index, value] of array)` or a regular `for` loop. – jfriend00 Jul 15 '19 at 02:27
-2

a is the increment, in your for in loop, so array[a] will give you the value in your case.

FYI... Technically, it's a bad idea to use a for in loop on an Array, because for in loops receive properties as Strings, it's just that Arrays that receive Numeric Strings as values cast those values to Numbers.

var array = [1, 2, 3];
for(var i in array){
  console.log(typeof i);
}

I would do my Array loops like:

var array = [1, 2, 3];
array.forEach(function(val, inc){
  console.log(typeof inc);
  console.log(val);
});
// or
console.log('-------');
for(var i=0,l=array.length; i<l; i++){
  console.log(array[i]);
  if(i === 1)break;
}
// in a function
console.log('-------');
var anotherArray = array.concat([4, 5, 6, 7, 8, 9]);
function breakBefore(array, breakBefore, startAt){
  var i = startAt === undefined ? 0 : startAt;
  for(l=array.length; i<l; i++){
    console.log('increment:'+i);
    console.log('value:'+array[i]);
    if(i === breakBefore){
      return;
    }
  }
}
breakBefore(anotherArray, 6, 2);
StackSlave
  • 10,613
  • 2
  • 18
  • 35