0

Unlike other multiple assignments in one line questions, this one is different in that:

  1. It's for assignments/updates
  2. It's about assignments/updates to hash attributes as well.

Take a look at the following code:

$ cat test.js
let a = b = "-"
console.log(a, b)
[a, b ] = ['abc', 'def']

const h = {a:"1", b:"2"}
console.log(h, h.a)
[h.a, h.b ] = ['abc', 'def']
console.log(h.a)

Everything works fine if running under node interactively:

> a = b = "-"
'-'
> console.log(a, b)
- -

> [a, b ] = ['abc', 'def']
[ 'abc', 'def' ]
> console.log(a, b)
abc def

> h = {a:"1", b:"2"}
{ a: '1', b: '2' }
> h
{ a: '1', b: '2' }
> [h.a, h.b ] = ['abc', 'def']
[ 'abc', 'def' ]
> h.a
'abc'

However, if running by node as script, or checking with eslint, there will be all kinds of problems.

Why running then under node interactively is OK but not if as a script?
Is it possible to fix it and make it working as script as well?

$ node -v
v16.18.1
xpt
  • 20,363
  • 37
  • 127
  • 216
  • Does this answer your question? [JS Declare and assign multi variable inLine es6?](https://stackoverflow.com/questions/49708393/js-declare-and-assign-multi-variable-inline-es6) – David Jones Jan 13 '23 at 19:49
  • What are the errors? – VLAZ Jan 13 '23 at 20:11
  • 1
    Use "use strict" and you'll get some errors. It is always a good habit to define *all* your variables with `let`, `const` or `var`. – trincot Jan 13 '23 at 22:03

0 Answers0