javascript interactive

JavaScript Interactive — a console crash course


2.5 Property Access (Objects)

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.
>>>
var o = { key: 'value' };


  
>>>
o.key
"value"
The dot (.) operator is used to access a property in an object.
>>>
o['key']
"value"
The array index syntax can also be used to access a property in an object. This syntax must be used when the name contains spaces or non alphanumeric characters.
>>>
var propName = 'key';


  
>>>
o[propName]
"value"
The index syntax is also used when the property name is calculated or stored in a variable
>>>
o.text = 1
1
New properties are added with assignment, using either of the two access syntaxes above.
>>>
o.doesNotExist
undefined
It is not an error to access a property that doesn't exist.
>>>
delete o.text
true
The delete operator removes properties (or global variables).
>>>

Index 1 2 3 4 5 6 7 8