Access JavaScript object property with hyphen
Question:
How to access JavaScript object property with hyphen? Answer:
let userBirthDate = user["birth-date"];
Description:
If you need to access an object property in JavaScript that contains a hyphen, dash '-'
, or space ' '
you can use the bracket notation syntax: object['property-name']
.
In general 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. Syntax:
object.property
- bracket notation: The property name is just a string or Symbol. So, it can be any string. Syntax:
object['property']
Reference:
JavaScript property accessors reference
Share "How to access JavaScript object property with hyphen?"
Related snippets:
Tags:
access, object, property, hyphen, dash, space, object key, javascript Technical term:
Access JavaScript object property with hyphen