-4

I have seen other questions asked on Stack Overflow about doing this, but I felt that they weren't answering the right question. They always answered how to access the name of an object with a string. However, I want to know a way to do this:

var obj = {
    property: "hello!"
}
console.log(obj."property");//want it to log hello!

1 Answers1

-1

You just access it like it's an array index, so:

console.log(obj["property"]);
aaronw
  • 136
  • 2
  • 9
  • "like it's an array index" - because arrays are objects will properties 0, 1, .... Since you can't do array.0, you use *bracket notation* – Andrew Li Mar 24 '17 at 04:31