Tristan Tzara Tristan Tzara - 10 months ago 51
Javascript Question

Is using for ...of for arrays ok?

Now we have a for... of loop in JS. Can it be used for iteration over arrays instead of

for (let i = 0, len = array.length; i < len; i++) {...}
seamlessly or are there any caveats that make using it for arrays a bad practice?

Answer Source

Yes, for... of is fine for Arrays because they are iterable.

You can verify that in your browser's console by checking that Array instances have a Symbol.iterator method:

> values() { [native code] }

... and, tautologically, by the fact that you can iterate them with for... of!