5

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78];
for(var index in arrayOfNumbers){
   console.log(index+1);
}

The output for this sample code is.
01
11
21
31
41
51
61

Why are these indexes of an array treated as a string in JavaScript?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rohitwtbs
  • 509
  • 5
  • 17
  • Because those are string.Try `typeof index` it returns `"string"` type – Deepak Kamat Mar 13 '18 at 07:11
  • 4
    keys of objects are always strings. – Nina Scholz Mar 13 '18 at 07:11
  • if you want add index with 1 then you can try this console.log(parseInt(index)+1); agree with @NinaScholz – sachin Mar 13 '18 at 07:12
  • A classic object has properties { 'foo' : 'hello' }, an array [ 'hello' ] is basically the same than { '0' : 'hello' }. Arrays are just objects where property names are indices, yet, it finally ends as a string. – sjahan Mar 13 '18 at 07:12
  • 1
    Yep, and `for..in` are originally meant for object, so it is wise to return a string as objects's keys are always string. – Deepak Kamat Mar 13 '18 at 07:12
  • @rohitwtbs i have posted a answer which can give you a expected out put. – SINGH Mar 13 '18 at 07:17
  • The index is not a string, ‘index+1’ is. Cast index to an int and you should be fine – patrick Mar 13 '18 at 07:18
  • 1
    BTW `for...in` loop should be avoided to use with arrays. See this [post](https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea). – Mohammad Usman Mar 13 '18 at 07:33
  • @DeepakKamat Not just originally. They still are :-) – Bergi Mar 22 '18 at 02:40

6 Answers6

3

From MDN for...in

Note: for...in should not be used to iterate over an Array where the index order is important.

... The for...in loop statement will return all enumerable properties, including those with non–integer names and those that are inherited.

When using for...in, the key is always a string and all it does is string concatenation.

You have an array, so better use Array.foreach() like so:

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78];
arrayOfNumbers.forEach(function(item, index){
     console.log(index + 1); // Here the index is a number!
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
caramba
  • 21,963
  • 19
  • 86
  • 127
0

A key is a string, because it is an object. In JavaScript there is Object and that's why its keys are in string.

You can achieve this by converting the string into int or add the prefix +.

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78];
for(var index in arrayOfNumbers) {
  var v = parseInt(index) + 1;
  console.log(v);
}

With a prefix

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78];
for(var index in arrayOfNumbers) {
  var v = parseInt(index) + 1;
  console.log(v);
}
 var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78];
 for(var index in arrayOfNumbers){
    var v = +index + 1;
     console.log(v);
  }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SINGH
  • 397
  • 1
  • 4
  • 16
0

Don’t use "for in" for an array. 'for in' is meant for traversing the object. When you try it with an array, it looks like this:

arr = {
    "0": 1,
    "1": 2,
    "2": 3,
    "3": 4,
    "4": 5,
    "5": 6,
    "6": 78
}

I hope you are looking for this:

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78];
for(var index=0; index<arrayOfNumbers.length; index++) {
     console.log(index+1);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Willson
  • 444
  • 1
  • 3
  • 13
0

Reference: MDN FOR..IN

The for...in statement iterates over the enumerable properties of an object. For each distinct property, statements can be executed.

var string1 = "";
var object1 = {a: 1, b: 2, c: 3};

for (var property1 in object1) {
  string1 = string1 + object1[property1];
}

console.log(string1);

In your case the indexes are referred to as object keys, which is a t string or a key string : { foo : 'something in universe'}

Have a look here, if you want the key's value:

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78];
for(var index in arrayOfNumbers) {
    console.log(arrayOfNumbers[index+1]);
}
console.log(arrayOfNumbers)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52
0

As stated in the comments and some answers, a for in loop is intended for object iterations, and in JavaScript arrays are objects as well. So the keys will be of type string.

However, for your intended result, you could use something like this:

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78];
arrayOfNumbers.forEach(function(num)
{
    console.log(num+1);
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Parthipan Natkunam
  • 756
  • 1
  • 11
  • 16
0

From for...in (MDN):

The for...in statement iterates over the enumerable properties of an object.

So the value of index in your code will always return the names of those properties. In an array, those are the names of the indexes.

Also, a String plus a Number equals a String which is what is printed in the console.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Meghan
  • 1,215
  • 11
  • 17