In JavaScript, if you type
typeof("2")
you should get string.
And if you type
typeof(2)
you should get number.
Now my question is, it is said that when you define an object, the key must be a string.
so this is legal
apple = {"color":"red"}
However, if I change the key to a number wrapped in the inverted commas, it should work as anything inside of inverted commas
apple={"2": "red"}
But If I call the object property, apple.2, this will fail with error Unexpected number.
so am just curious, what am I missing here while defining the objects in JavaScript.
typeofis an operator, not a function, so the parentheses are unnecessary. The object's key must be a string, so you'd need to access it viaapple["2"], not using a number viaapple.2.2is not a valid JS identifier.