0

The question is not only about the posiblity of the numerical object keys but also about the number variable is a numeric or string (Coercion)? as JSelser stated.

I wonder how this is posible:

var name = {1:"Bob",2:"June",3:"Catty"};
var number = String(Math.floor(Math.random()*Object.getOwnPropertyNames(name).length)+1);
console.log(name[number]);

or so:

var name = {"1":"Bob","2":"June","3":"Catty"};
var number = String(Math.floor(Math.random()*Object.getOwnPropertyNames(name).length)+1);
console.log(name[number]);

or so:

var name = {"1":"Bob","2":"June","3":"Catty"};
var number = Math.floor(Math.random()*Object.getOwnPropertyNames(name).length)+1;
console.log(name[number]);

Note how first key has no quotation marks and the number variable is equated to a string or to a number, but in all ones the result is ok.

Hender
  • 343
  • 2
  • 10

0 Answers0