Can JavaScript Object Property Name Have Underscores?

Find out if underscores in JavaScript property name are allowed

JavaScript object property names can be any string, including having underscores in the name. In fact, underscores are valid identifiers, which means that:

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.