0

I couldn't find a similar thread as to this specific case, so I decided to make one.

Basically, let's consider this snippet.

let a = [7];
const b = a;

a[0] = a = 15;

console.log(a, b) // 15 [15]

According to MDN, the assignment operator goes from right to left, so I figured 15 is assigned to a, which means a now will point to a primitive, so going to the left, a[0] shouldn't be able to modify the array element, since it's not pointing at the reference anymore. Unless I am missing something here. Maybe someone would like to explain step by step how this goes? Thank you very much.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 1
    "*a[0] shouldn't be able to modify the array element, since it's not pointing at the reference anymore*" but it's not processed as `a[0] = a` because *first* `a[0]` is retrieved thus the assignment is happening directly to that value. It's not an assignment "to the property of variable `a` ". – VLAZ Aug 31 '23 at 06:41
  • 1
    @RobbyCornelissen not really the same, though? `a[0] = a = 15` assigns the value to the array. It's not resolved to assign a "property" to a primitive. – VLAZ Aug 31 '23 at 06:47
  • @VLAZ First `15` gets assigned to `a`; `a` is now `15`. Then the result of this expression (`15`) gets assigned to `a[0]`; since `a` is `15` at this point, it's exactly what Robby describes and does nothing. – deceze Aug 31 '23 at 06:48
  • 1
    @deceze no, the processing is to first evaluate the `a[0] = ` part which retrieves the value assigned to `a` which is an array. *Then* the rest of the expression is processed. It's processed right-to-left, resolved "sort of" left-to-right. Just because the assignment to that array cannot happen before the value in is derived. – VLAZ Aug 31 '23 at 06:51
  • @RobbyCornelissen do you not see that it *does* by running the snippet? The result isn't `15 [7]` as you imply, – VLAZ Aug 31 '23 at 06:52
  • 2
    @VLAZ It doesn't work that way. `a[0] = ...` resolves first, means **assigning the first element of `the array` to...**. Even though `a` got assigned to another value later, it doesn't change the fact that it's in the middle of the process of *assigning the first element of **`the array`***. That's why `b` is `[15]`, since variable `b` is pointing to **`the array`** all the time. – Hao Wu Aug 31 '23 at 06:52
  • 3
    I'm confused how three people can be telling me I'm wrong in the face of 1. a dupe claiming the opposite 2. the snippet clearly showing the behaviour is not what they claim it is... – VLAZ Aug 31 '23 at 06:53
  • 1
    @VLAZ Ah, you're right, the `a[0]` gets "prepared" first, even if `a` changes out from under it… – deceze Aug 31 '23 at 06:54
  • 1
    it works like this: `a[0] = (a = 15)` – Nina Scholz Aug 31 '23 at 06:55
  • 1
    @RobbyCornelissen what?! The triple negation doesn't help me understand why you think your explanation fits given the result doesn't fit your explanation. – VLAZ Aug 31 '23 at 06:56
  • @VLAZ In light of recently presented evidence, I'll carefully reevaluate my position :) – Robby Cornelissen Aug 31 '23 at 06:58
  • And admit that you're right, and I'm wrong. – Robby Cornelissen Aug 31 '23 at 06:59
  • Might be personal, but I find it easier to visualize thinking of it as `Reflect.set(a, "0", (a = 15))` So the `set` method will get in usual left to right order, access to: its own pointer to `a`, the right key to work with, and whatever the value is. – Kaiido Aug 31 '23 at 07:23
  • I think I was meant to mention Robby, on well. – Hao Wu Aug 31 '23 at 07:23

0 Answers0