how to get object’s name in javascript?

You can’t. You’re only passing the object { black : 'bad', gray : 'not so bad' } into test. This object does not intrinsically have the name “dark”, it’s just an object that happened to exist as the property dark of the object a. This information is irretrievably lost when passing it into a function.

You’re basically trying to retrieve the variable name that held the value before the value got passed into the function. That’s not possible.

Leave a Comment