0

My origin test, try to assign 2 numbers to the object.

let obj = {}
let num = 100
let tar = 200

[obj.num, obj.tar] = [num, tar]
console.log(obj)

There is a reference error that I can't figure out.

VM212:5 Uncaught ReferenceError: tar is not defined
    at <anonymous>:5:28

However next, stranger thing happen.

let obj = {}
let num = 100
let tar = 200
let a 
let b
[obj.num, obj.tar] = [num, tar]
console.log(obj)

It logged successfully.

{num: 100, tar: 200}

Please help me out of this.

avocadoLambda
  • 1,332
  • 7
  • 16
  • 33
高欣平
  • 172
  • 2
  • 8

1 Answers1

0

Just add a semicolon add the end of each line. This makes it easier for the JavaScript interpreter/compiler to understand what you mean.

let obj = {};
let num = 100;
let tar = 200;

[obj.num, obj.tar] = [num, tar];
console.log(obj)
Mario Varchmin
  • 3,704
  • 4
  • 18
  • 33