Let's say I have

myArray = ['item1', 'item2']

I tried

for (var item in myArray) {console.log(item)}

It prints 0 1

What I wish is to have item1 item2

Is there any other syntax that works without using

for (var i = 0; i < myArray.length; i++)
share|improve this question
2  
What are you trying to achieve? Why not use a normal for loop or the .forEach() function? – Pointy Apr 1 '15 at 18:09
    
Writing it as for (var propertyName in myArray) would make the issue with the observed behavior more clear. In any case, for a higher-order construct, see Array.forEach but note it has a number of implications that make it different from either of the above forms. (The differences between the forms are covered by many duplicates.) – user2864740 Apr 1 '15 at 18:10
1  
@Pointy because the normal loop is uselessly complicated : why do I need to calculate the size of an array whereas the interpreter can know it. forEach() ? Well that's my question is there any other syntax so I'll try thanks. – user310291 Apr 1 '15 at 18:11
7  
@user310291 length doesn't "calculate" the size of the array. The length of an array is an intrinsic value of the Array type. – user2864740 Apr 1 '15 at 18:13
3  
Even if length was calculated, you could just cache it: for (var i = 0, length = arr.length; i < length; i++) – Felix Kling Apr 1 '15 at 18:23
up vote 52 down vote accepted

You can use Array.forEach

var myArray = ['1','2',3,4]

myArray.forEach(function(value){
  console.log(value);
});

share|improve this answer
3  
Still uses array length under the hood. But it could simply be that the OP doesn't want to refer to the length explicitly. – canon Apr 1 '15 at 18:18
    
@canon, you literally suggested forEach() (after I answered here) in the comment for another answer. – makenova Apr 1 '15 at 18:22
1  
Note that if you add myArray[123] = '6' you will get a totally different result using Array.forEach than when using for (var i = 0; i < myArray.length; i++) syntax. – borisdiakur Apr 1 '15 at 18:48
    
Oh, this comment. I originally thought you meant this one. That was mainly because, if reaching for an array method at all, map() wouldn't be the appropriate analog. – canon Apr 1 '15 at 18:51

What you probably want is for...of, a relatively new construct built for the express purpose of enumerating the values of iterable objects:

var myArray = ["a","b","c","d"];
for (let item of myArray) {
  console.log(item)
}

Your original loop didn't display the expected results because for...in enumerates property names (presumably1 numeric indices in the case of arrays) rather than values. So, you missed the step of getting the actual property value, by name, via bracket notation.

var myArray = ["a","b","c","d"];
for (var key in myArray) {
  console.log("key: %o, value: %o", key, myArray[key])
}

1 Unfortunately, someone may have added enumerable properties to the array or its prototype chain which are not numeric indices... or they may have assigned an index leaving unassigned indices in the interim range. The issues are explained pretty well here. The main takeaway is that it's best to loop explicitly from 0 to array.length - 1 rather than using for...in.

So, this is not (as I'd originally thought) an academic question, i.e.:

Without regard for practicality, is it possible to avoid length when iterating over an array?

According to your comment (emphasis mine):

[...] why do I need to calculate the size of an array whereas the interpreter can know it.

You have a misguided aversion to Array.length. It's not calculated on the fly; it's updated whenever the length of the array changes. You're not going to see performance gains by avoiding it (apart from caching the array length rather than accessing the property):

loop test

Now, even if you did get some marginal performance increase, I doubt it would be enough to justify the risk of dealing with the aforementioned issues.

share|improve this answer
    
Yes, but it should be pointed out that this is generally not the preferred way of iterating through arrays. – Pointy Apr 1 '15 at 18:08
1  
@DaveNewton I think that was a typo. – Pointy Apr 1 '15 at 18:10
3  
2  
@March he tried and failed because he printed the key instead of looking up the value by key in the array. That speaks to a fundamental misunderstanding of for...in on top of his unwarranted aversion to length. – canon Apr 1 '15 at 18:30
1  
I like your stack-snippet-console, nice work! : ] – borisdiakur Apr 1 '15 at 20:10

To print 'item1' , 'item2', this code would work.

var myarray = ['hello', ' hello again'];

for (var item in myarray) {
    console.log(myarray[item])
}
share|improve this answer

In ES5 there is no efficient way to iterate over a sparse array without using the length property. In ES6 you can use for...of. Take this examples:

'use strict';

var arr = ['one', 'two', undefined, 3, 4],
    output;

arr[6] = 'five';

output = '';
arr.forEach(function (val) {
    output += val + ' ';
});
console.log(output);

output = '';
for (var i = 0; i < arr.length; i++) {
    output += arr[i] + ' ';
}
console.log(output);

output = '';
for (var val of arr) {
    output += val + ' ';
};
console.log(output);
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->
<script src="//gh-canon.github.io/stack-snippet-console/console.min.js"></script>

All array methods which you can use to iterate safely over dense arrays use the length property of an object created by calling ToObject internaly. See for instance the algorithm used in the forEach method: http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.18 However in es6, you can use for...of safely for iterating over sparse arrays.

See also Are Javascript arrays sparse?.

share|improve this answer

Use the built-in Javascript function called map. .map() will do the exact thing you're looking for!

share|improve this answer
    
I would think forEach() would be the more direct match than a function which creates a new array... – canon Apr 1 '15 at 18:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.