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.

Peter
  • 1,221
  • 6
  • 14
  • 31
高欣平
  • 111
  • 2
  • 5

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
  • 2,959
  • 3
  • 15
  • 28