# Kotlin Iterator - range-based for loop in JavaScript

**URL:** <https://discuss.kotlinlang.org/t/kotlin-iterator-range-based-for-loop-in-javascript/3675>\
**Category:** JavaScript\
**Created:** [July 10, 2017, 2:43am UTC](https://discuss.kotlinlang.org/t/kotlin-iterator-range-based-for-loop-in-javascript/3675 "2017-07-10T02:43:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrejpetelin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andrejpetelin/32/3130_2.png) [@andrejpetelin](https://discuss.kotlinlang.org/u/andrejpetelin)\
**Post date:** [July 10, 2017, 2:43am UTC](https://discuss.kotlinlang.org/t/kotlin-iterator-range-based-for-loop-in-javascript/3675/1 "2017-07-10T02:43:04Z")

</div>

Hello. I am implementing some JS modules for node.js in Kotlin, I am really enjoying the experience so far, so thanks for the brilliant work on a brilliant language.

However one type of the library that’s supposed to be used from JavaScript is a MutableIterable. What I’ve noticed is that Kotlin code like this:  
`for (i in smi) println(i.toString())`

gets transpiled to JavaScript like this:

```
while (tmp$.hasNext()) {
  var j = tmp$.next();
  println(j.toString());
}

```

instead of a JS range-based for loop like `for (val i of smi)`

While the Kotlin MutableIterator/MutableIterable work just fine on the JVM I cannot actually use a range-based for loop on the Kotlin module in node. Is this a thing or am I just missing something? Thanks!

---

<div class="post-metadata">

**Author:** ![Alexey.Andreev](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Alexey.Andreev](https://discuss.kotlinlang.org/u/Alexey.Andreev)\
**Post date:** [July 10, 2017, 8:14am UTC](https://discuss.kotlinlang.org/t/kotlin-iterator-range-based-for-loop-in-javascript/3675/2 "2017-07-10T08:14:45Z")

</div>

Kotlin does not support emitting ES6+, so it won’t generate `for..of`. Currently, `for..in` in Kotlin works via Kotlin iterators producing the JS code you mentioned, it should work. Do you have any problems with it?

---

<div class="post-metadata">

**Author:** ![andrejpetelin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andrejpetelin/32/3130_2.png) [@andrejpetelin](https://discuss.kotlinlang.org/u/andrejpetelin)\
**Post date:** [July 10, 2017, 12:28pm UTC](https://discuss.kotlinlang.org/t/kotlin-iterator-range-based-for-loop-in-javascript/3675/3 "2017-07-10T12:28:34Z")

</div>

@Alexey.Andreev, ah, of course, that makes sense. Thank you.

Well, since it’s a priority queue I decided it better to add a popAll method instead, but will try it out when I can get around to it and let you know if there is problem. Thank you for the reply 🙂

---

<div class="post-metadata">

**Author:** ![irenenaya](https://avatars.discourse-cdn.com/v4/letter/i/6bbea6/32.png) [@irenenaya](https://discuss.kotlinlang.org/u/irenenaya)\
**Post date:** [July 10, 2017, 10:15pm UTC](https://discuss.kotlinlang.org/t/kotlin-iterator-range-based-for-loop-in-javascript/3675/4 "2017-07-10T22:15:33Z")

</div>

@Alexey.Andreev : Thanks for the reply. Just to make one thing clear: If we define a custom iterator for a class in Kotlin, and then we transpile it to Javascript (using CommonJS in the settings), is there any way in which then we can actually iterate that object with a for…in or for…of loop?  
for…of loop gives an error in javascript, and for…in doesn’t crash but it doesn’t actually iterate the object.  
As a very small example:

```
class MyIterable() : Iterable<Int> {
    var data : ArrayList<Int> = arrayListOf()
    fun push(x : Int) {
        data.add(x)
    }
    override fun iterator() : Iterator<Int> = data.iterator()
}

```

If i then declare an object of this class and push a couple of items, doing

`for (i in myObj) println(i)`

This will work fine in Kotlin, of course. But the transpiled code will not actually iterate in Javascript. If i do

`for (var i in myObj) console.log(myObj[i]);`

It gives an output like this:

```
ArrayList { modCount: 3, 'array_9xgyxj$_0': [2, 3, 4] }
[Function]
[Function]
[Function]

```

Also tried using Kotlin iterators created with the AbstractIterator and MutableIterator classes. From your answer, I guess that it’s not possible for now to create an iterator that will work as such in Javascript, but just wanted to make sure. Thanks! 🙂
