Is this allowed in javascript ?
a = {undefined : 1}
console.log(a[undefined])
[Edit] I think I was a bit messed up with habits of python, but I actually though about this one :
a = {[undefined] : 1}
console.log(a[undefined])
undefined is casted as a string (like any value you pass with this synthax) and you end up defining the "undefined" prop
[/Edit]
And if so, does all browsers handle it the same and is it explicitly specified in the standards ?
[Edit]
NOTE :
this question was slightly different from 'undefined' variable works as key to object with 'undefined' property name even though the fundamental reason and answer are the same.
I asked in a context where undefined seems ambiguous as "is it the value undefined or the string "undefined" ? ", while the linked question state clearly "undefined" as a a prop name and asks about the name collision with keyword.
...And I think it can help other programmers used to Python or other language with json-like object/dict/hash synthax
[/Edit]
Is this allowed in javascript ?
Yes.
And if so, does all browsers handle it the same
Yes.
is it explicitly specified in the standards ?
No.
It's just a consequence of undefined being converted to "undefined" when used in string context:
console.log( ("" + undefined).toUpperCase() )
Object property names are always strings. So whatever you pass as a property name gets converted to a string. If you pass undefined then it becomes "undefined". So this:
let obj = { undefined: 0 }
obj[undefined]
is the same as this
let obj = { undefined: 0 }
obj["undefined"]
undefined doesn't have a special meaning in that case. The same applies to null, false, true etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With