today at work a friend show me a strange way to access an object attribute using a variable.
To make it simple to understand, first we define an object, then a variable with a key of this object as string, then we try to access this key on the object.
object = {"123": 1, "12":2}
key = "12"
console.log(object[key])
this would return 2 and is the right way to do.
So my friend was doing this on react
object = {"123": 1, "12":2}
key = "12"
console.log(object.[key])
Trying this code on webbrowser console, returns error, but on React it works.
Can someone tell me why???? I'm really curious to know why this no sense code works
ready snippet to try on pure js
.) or bracket notation ([]). You can't use both for accessing a single propertyarr?.[index]