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

You can access JavaScript object properties that have spaces in the name 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 spaces 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.