Access JavaScript object property with space
Question:
How to access JavaScript object property with space? Answer:
let user = users["John Doe"];
Description:
If you need to access an object property in JavaScript that contains one or more spaces you can use the bracket notation syntax: object['property name']
.
There are 2 types of property accessors in JavaScript that provide access to an object's properties:
- dot notation: The property must be a valid JavaScript identifier. So space is not allowed in the name. Syntax:
object.property
- bracket notation: The property name is just a string or Symbol. So, it can be any string. Syntax:
object['pro per ty']
Reference:
Property accessors reference
Share "How to access JavaScript object property with space?"
Related snippets:
Tags:
access, object, property, space, spaces, object key, javascript, space in property name, space in key Technical term:
Access JavaScript object property with space