Ben Aston
Ben Aston

Reputation: 55759

Is the result of an assignment expression always the value on the right-hand side?

In the following code, the object o is assigned to foo (after the destructuring).

In other words, the result of the assignment expression ({ bar, bam } = o) is o.

const o = { bar: '', bam: '' }
const foo = ({ bar, bam } = o)
console.log(foo === o) // true

Is the result of an assignment expression always the value of the expression on the right-hand side?

Upvotes: 2

Views: 59

Answers (2)

qiAlex
qiAlex

Reputation: 4346

({ bar, bam } = o) returns o so foo is a link to o

Upvotes: 1

Pointy
Pointy

Reputation: 413846

It's all in section 12.14.4 of the spec. For the "plain" assignment operator =, the value is always the right-hand side. For assignment operators that compute a result, like +=, the value of the overall expression is the computed result.

Upvotes: 3

Related Questions