# Range Operator

**URL:** <https://es.discourse.group/t/range-operator/1784>\
**Category:** 💡 Ideas\
**Created:** [August 22, 2023, 3:03pm UTC](https://es.discourse.group/t/range-operator/1784 "2023-08-22T15:03:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ethanlal04](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ethanlal04/32/918_2.png) [@ethanlal04](https://es.discourse.group/u/ethanlal04)\
**Post date:** [August 22, 2023, 3:03pm UTC](https://es.discourse.group/t/range-operator/1784/1 "2023-08-22T15:03:11Z")

</div>

```javascript
for (let i = 0; i < 10; i += 2) {
  // Whatever
}

```

I mean, come on. It's 2023! Why do we still have to write code like that?

Python, for e.g., has its awesome `range()` function which returns an iterable sequence of numbers within the provided range. Rust also has a range operator (`0..10`) that does the same thing.

JS too could really use a range operator.

One that would return a new special `Range` iterable which could be looped over like

```javascript
for (let i of :10:2) {
  // Whatever
}

```

Isn't that cool? A few examples for you to catch the semantics

```javascript
1:10 // 1, 2, 3, ..., 9
:10 // 0, 1, 2, ..., 9
10: // 10, 11, 12, ..., Infinity
: // 0, 1, 2, ..., Infinity
:10:2 // 0, 2, 4, ..., 9
1:10: // 1, 2, 3, ..., 9
::2 // 0, 2, 4, ..., Infinity
:: // You know this

```

Very like Python's array slice notation.  
Speaking of arrays, it's also quite common to need an array with an arithmetic sequence of numbers. Maybe we could 'magically spread' the `Range` iterable into an array for that?

```javascript
const numbers = [...1:10] // [1, 2, 3, ..., 9]

```

That's two birds with one stone. What do u think?

---

<div class="post-metadata">

**Author:** ![Josh-Cena](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh-cena/32/1408_2.png) [@Josh-Cena](https://es.discourse.group/u/Josh-Cena)\
**Post date:** [August 22, 2023, 3:49pm UTC](https://es.discourse.group/t/range-operator/1784/2 "2023-08-22T15:49:46Z")

</div>

> **[GitHub - tc39/proposal-iterator.range: A proposal for ECMAScript to add a...](https://github.com/tc39/proposal-iterator.range)**
>
> A proposal for ECMAScript to add a built-in Iterator.range() - GitHub - tc39/proposal-iterator.range: A proposal for ECMAScript to add a built-in Iterator.range()

---

<div class="post-metadata">

**Author:** ![ethanlal04](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ethanlal04/32/918_2.png) [@ethanlal04](https://es.discourse.group/u/ethanlal04)\
**Post date:** [August 24, 2023, 12:48pm UTC](https://es.discourse.group/t/range-operator/1784/3 "2023-08-24T12:48:47Z")

</div>

Yeah, nice to see that people have given the problem attention. All the same, I would prefer an operator over a function.

---

<div class="post-metadata">

**Author:** ![Josh-Cena](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh-cena/32/1408_2.png) [@Josh-Cena](https://es.discourse.group/u/Josh-Cena)\
**Post date:** [August 24, 2023, 4:06pm UTC](https://es.discourse.group/t/range-operator/1784/4 "2023-08-24T16:06:16Z")

</div>

Syntax has a much higher barrier to entry, so unless there's a stronger motivation than "it saves a few characters" it's unlikely to get approval.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [August 24, 2023, 4:58pm UTC](https://es.discourse.group/t/range-operator/1784/5 "2023-08-24T16:58:32Z")

</div>

[[a..b] and [a..n..b] syntax · Issue #20 · tc39/proposal-iterator.range · GitHub](https://github.com/tc39/proposal-iterator.range/issues/20) covers some syntax suggestions. The issue concluded with:

> I think if a new syntax is really necessary, we can add it in the future after all. The current proposal should focus on the API shape and runtime semantics.
