# Support references to array indices.

**URL:** <https://es.discourse.group/t/support-references-to-array-indices/2427>\
**Category:** 💡 Ideas\
**Created:** [August 27, 2025, 4:12pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427 "2025-08-27T16:12:22Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![7ombie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/7ombie/32/1631_2.png) [@7ombie](https://es.discourse.group/u/7ombie)\
**Post date:** [August 27, 2025, 4:12pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/1 "2025-08-27T16:12:22Z")

</div>

Working with typed arrays is clunky, because we cannot reference values in arrays directly. It’d be handy if we could do something like this:

```js
const bytes = new UintArray(256);
const x = 0 of bytes;

console.log(x); // console.log(bytes[0]);
x = 1; // bytes[0] = 1;

```

The `of` operator would expect an index expression on the left and an array expression on the right.

This could be generalized to any array or sequence etc, but it's principally about working with typed data more naturally, so could be limited to typed arrays.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [August 27, 2025, 4:30pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/2 "2025-08-27T16:30:05Z")

</div>

I'm confused, you already can both get and set `bytes[0]`.

---

<div class="post-metadata">

**Author:** ![7ombie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/7ombie/32/1631_2.png) [@7ombie](https://es.discourse.group/u/7ombie)\
**Post date:** [August 27, 2025, 4:38pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/3 "2025-08-27T16:38:28Z")

</div>

Yeah, but you have to reference the array and index (`bytes[index]`), which gets messy in practice.

Typed arrays provide a indirect solution to typed values in JavaScript. Support for _indirect references_ would make the indirection automatic (so `x` can act more like a reference to typed value).

---

<div class="post-metadata">

**Author:** ![tabatkins](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tabatkins/32/170_2.png) [@tabatkins](https://es.discourse.group/u/tabatkins)\
**Post date:** [August 27, 2025, 5:01pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/4 "2025-08-27T17:01:05Z")

</div>

…I’m also confused. Your example code in the first post _also_ references the array and index.

It looks like your actual request is completely unconnected to TypedArray, and is actually about the ability to take references into data structures?

---

<div class="post-metadata">

**Author:** ![7ombie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/7ombie/32/1631_2.png) [@7ombie](https://es.discourse.group/u/7ombie)\
**Post date:** [August 27, 2025, 5:23pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/5 "2025-08-27T17:23:05Z")

</div>

You only reference the array and index once (`const x = index of array`). Thereafter, you can use `x`, instead of having to write `array[index]` over and over again. This makes sense if you work with typed arrays a lot.

You could generalize it to allow references to values within any structure, but that just feels a bit wacky. This is much more natural in the context of typed arrays, where we're often only using an array to store typed values. The values are not _arrayed_ in any meaningful sense. They're just stored in an array to allow for typed numbers (and optimized math).

---

<div class="post-metadata">

**Author:** ![7ombie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/7ombie/32/1631_2.png) [@7ombie](https://es.discourse.group/u/7ombie)\
**Post date:** [August 27, 2025, 5:31pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/6 "2025-08-27T17:31:08Z")

</div>

If _reference indirection_ was supported on regular arrays etc, I would maybe use that feature once in a while, but I could easily live without it. It is much more convenient for working with typed arrays.

Also, if reference indirection was generally supported for any structure, beginner programmers would need to deal with it fairly early on. If it's limited to typed arrays, you only need to know about it when you work with typed arrays, and it makes a lot more sense in that context.

I'm not strongly _against_ generalizing it to all structures. I just think it's debatable.

---

<div class="post-metadata">

**Author:** ![7ombie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/7ombie/32/1631_2.png) [@7ombie](https://es.discourse.group/u/7ombie)\
**Post date:** [August 27, 2025, 5:50pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/7 "2025-08-27T17:50:14Z")

</div>

For example, you could do stuff like this:

```js
const i = 0 of new Uint8Array(1);

```

And then use `i` to reference a `u8`, without needing a reference to the array that only exists to store the `u8`.

---

<div class="post-metadata">

**Author:** ![tabatkins](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tabatkins/32/170_2.png) [@tabatkins](https://es.discourse.group/u/tabatkins)\
**Post date:** [August 27, 2025, 11:19pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/8 "2025-08-27T23:19:13Z")

</div>

> If _reference indirection_ was supported on regular arrays etc, I would maybe use that feature once in a while, but I could easily live without it. It is much more convenient for working with typed arrays.

I don’t understand why this would be the case. Arrays and Typed Arrays (and objects) are _exactly identical_ in this regard. You use `arr[index]` for arrays, `arr[index]` for typed arrays, and `obj[key]` for objects.

The only thing I can come up with is that you’re using TypedArrays not as arrays at all, but just as a way to force a value to be a particular numeric type?

---

<div class="post-metadata">

**Author:** ![7ombie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/7ombie/32/1631_2.png) [@7ombie](https://es.discourse.group/u/7ombie)\
**Post date:** [August 28, 2025, 12:44am UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/9 "2025-08-28T00:44:48Z")

</div>

> The only thing I can come up with is that you’re using TypedArrays not as arrays at all, but just as a way to force a value to be a particular numeric type?

@tabatkins - Exactly. That's one of the usecases for typed arrays. Expressions on values in typed arrays of the same type are (highly) optimized.

For example, if `uints` was a `Uint32Array`, then `uints[0] = uints[1] + uints[2]`) would use unsigned 32-bit operands and result (without converting to and from `Number`). It'd be nice to be able to write `x = y + z` instead.

---

<div class="post-metadata">

**Author:** ![swhiteman](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/swhiteman/32/716_2.png) [@swhiteman](https://es.discourse.group/u/swhiteman)\
**Post date:** [August 28, 2025, 2:55am UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/10 "2025-08-28T02:55:38Z")

</div>

Not that I would ever do this but

```javascript
const bytes = new Uint8Array(256);
const byte99 = Reflect.get.bind(Reflect.get,bytes,99);

bytes[99] = 1;
console.log(byte99()); // 1
    
bytes[99] = 2;
console.log(byte99()); // 2

```

---

<div class="post-metadata">

**Author:** ![juner](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/juner/32/2377_2.png) [@juner](https://es.discourse.group/u/juner)\
**Post date:** [August 28, 2025, 5:49am UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/11 "2025-08-28T05:49:08Z")

</div>

If we expand on the syntactic sugar, it would probably look something like this:

```javascript
const bytes = new UintArray(256); 
// const x = 0 of bytes; 
const x = Object.freeze({
    get value() { return bytes[0]; },
    set value(_) { bytes[0] = _; }
});

// console.log(x); 
console.log(x.value);         
// x = 1;
x.value = 1;                  

```

It seems to be something similar to `proposal refs`?

[rbuckton/proposal-refs: Ref declarations and expressions for ECMAScript](https://github.com/rbuckton/proposal-refs)

---

<div class="post-metadata">

**Author:** ![blob](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/blob/32/2230_2.png) [@blob](https://es.discourse.group/u/blob)\
**Post date:** [August 29, 2025, 8:55am UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/12 "2025-08-29T08:55:32Z")

</div>

People will do anything but learn C++

---

<div class="post-metadata">

**Author:** ![7ombie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/7ombie/32/1631_2.png) [@7ombie](https://es.discourse.group/u/7ombie)\
**Post date:** [September 3, 2025, 2:13pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/13 "2025-09-03T14:13:35Z")

</div>

You write webapps in C++ ??

I like Swift, but not in the browser.

---

<div class="post-metadata">

**Author:** ![7ombie](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/7ombie/32/1631_2.png) [@7ombie](https://es.discourse.group/u/7ombie)\
**Post date:** [September 3, 2025, 2:42pm UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/14 "2025-09-03T14:42:20Z")

</div>

You're right. I didn't know about `proposal refs`, but that would cover this usecase nicely. Still, something as general as `refs` might be a bridge too far.

---

<div class="post-metadata">

**Author:** ![blob](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/blob/32/2230_2.png) [@blob](https://es.discourse.group/u/blob)\
**Post date:** [September 4, 2025, 12:30am UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/15 "2025-09-04T00:30:12Z")

</div>

we have wasm in the big 25

---

<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:** [September 5, 2025, 4:53am UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/16 "2025-09-05T04:53:46Z")

</div>

Good luck writing an actually performant web application in wasm; have you?

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [September 5, 2025, 6:12am UTC](https://es.discourse.group/t/support-references-to-array-indices/2427/17 "2025-09-05T06:12:54Z")

</div>

Please keep posts on topic.
