JavaScript object property names can be any string, including having underscores in the name. In fact, underscores are valid identifiers, which means that:
- Property names can be specified without quotes;
- Properties can be accessed using either the dot notation (i.e.
object.property
syntax) or the bracket notation (i.e.object['property']
syntax).
For example, the following is a valid property name:
const obj = { foo_bar: 'baz' }; console.log(obj.foo_bar); // 'baz' console.log(obj['foo_bar']); // 'baz'
You can even have underscores at start or the end of the property name:
const obj = { _foo_: 'bar' }; console.log(obj._foo_); // 'bar' console.log(obj['_foo_']); // 'bar'
Hope you found this post useful. It was published . Please show your love and support by sharing this post.