How to Access JavaScript Object Property That Has Hyphens in the Name?

You can access JavaScript object properties that have hyphens in the name (a.k.a. kebab case) by using the bracket property accessor notation, like so:

const obj = { 'foo-bar': 123 };

console.log(obj['foo-bar']); // 123

It's the same even if the object property name has hyphens at start or the end:

const obj = { '--foo-bar-': 123 };

console.log(obj['--foo-bar-']); // 123

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.