# range()

[JS](../..)⟩ [iteration](..) ⟩ [iterable](../iterable) ⟩ [extension]() ⟩ range()&#x20;

{% hint style="success" %}
([iterable](../iterable "mention")) half-open range of numbers.

```javascript
range(0, 5),        // 0, 1, 2, 3, 4
range(0, 10, 2),    // 0, 2, 4, 6, 8
range(1, 2, 0.3),   // 1, 1.3, 1.6, 1.9000000000000001
range(2, -3),       // (empty iterable)
```

:floppy\_disk: replit ⟩ [range()](https://replit.com/@pegasusroe/range#index.js)&#x20;
{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}
* replit ⟩ [Iterable (protocol)](https://replit.com/@pegasusroe/Iterable-protocol#ext/Iterable.js)&#x20;

```javascript
// ⭐️ half-open range: [start, end)
function range(start, end, step = 1) {
    // return an iterable
    return {
        *[Symbol.iterator]() {
            for (let i = start; start <= i && i < end; i += step) {
                yield i;
            }
        }
    }
}
```
{% endtab %}

{% tab title="👥 相關" %}
* [closedrange](../iterable/custom/closedrange "mention") / [closedrange](../generator/examples/closedrange "mention") / [sequence](../iterable/custom/sequence "mention") / [seq](../generator/examples/seq "mention")
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [javascript-the-definitive-guide](../../../master/ref/javascript-the-definitive-guide "mention") ⟩ 12.2 Implementing Iterable Objects&#x20;
{% endtab %}
{% endtabs %}
