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

Learn how you can access object properties with spaces in the name in JavaScript

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

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