How to Access Snake Case Object Property Value in JavaScript?

You can access JavaScript object properties that are in snake case, using the dot notation or bracket notation:

const obj = { '_foo_': 'bar', 'foo_bar': 'baz' };

console.log(obj._foo_); // 'bar'
console.log(obj['_foo_']); // 'bar'

console.log(obj.foo_bar); // 'baz'
console.log(obj['foo_bar']); // 'baz'

This works with both, the dot notation and the bracket notation, because:

  • The dot notation requires the property to be a valid JavaScript identifier (which underscore is);
  • The bracket notation works with any string.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.