I
I
Ivan the Terrible2015-07-28 20:29:52
JavaScript
Ivan the Terrible, 2015-07-28 20:29:52

Objects in JavaScript. Why is 1 displayed and not 0?

var obj = {'1': 0, 1: 1, 0: 2};
alert(obj['1']);
Why is the value of property 1 displayed and not property '1' ?
We are referring to the property '1', not 1.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2015-07-28
@warriorkg

Because the key is always a string. Any other types are cast to string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question