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.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.