so I've been getting confused about this particular issue, hopefully someone more experienced can help me out on this.
Why does this work:
This will add the value as a key, and the value of the key to true. So if you say something like this
const mySet = new Set()
mySet.add("hello")
it will return this: {hello: true}.
But if I do something like this outside ES6 classes, so more like this:
const car = {
color: "red"
}
car[built] = 2019
This will say built is undefined, is this only usable in ES6 classes?
car[built] = 2019 built is an undefined variable
Change it to car["built"] = 2019 or car.built = 2019
var car = {};
var built = "some key";
car[built] = true;
then car is {"some key": true}
It works in the class syntax as you have defined a method that accepts a variable that is used as a key, and thus successfully creates a key in the object.
In the car[built] syntax it considers built here as a variable and thus fails in this case.
Whenever defining a key using obj[key] syntax, the key should either be a value or a string.