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?
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?
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!
});
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);
}
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);
}
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)
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);
});
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
.