Suppose I have an object
obj = {
a : 1
}
I'm able to access property a
via obj["a"]
but I'm also able to access it via obj[["a"]]
. How is that possible?
Suppose I have an object
obj = {
a : 1
}
I'm able to access property a
via obj["a"]
but I'm also able to access it via obj[["a"]]
. How is that possible?
Object keys are always strings (or, rarely, symbols). When you do
obj[<expression>]
the interpreter will try to turn expression
into a valid key, if it isn't one already. In this case, turning ["a"]
into a string results in "a"
, so both obj["a"]
and obj[["a"]]
work.
(When an array is implicitly turned into a primitive, like here, it gets .join
ed by a comma, and ["a"].join(',') === "a"
)