Can JavaScript Object Property Name Have Spaces?

Find out if it is valid to have spaces in a JavaScript object property name

JavaScript object property names can be any string, including having spaces in the name. However, object property names that are not valid JavaScript identifiers, can only be:

  1. Specified using quotes, and;
  2. Accessed using the bracket property accessor notation.

For example, the following is a valid property name:

const obj = { 'Foo Bar': 123 };

console.log(obj['Foo Bar']); // 123

You can even have spaces at start or the end of the property name:

const obj = { ' Foo Bar ': 123 };

console.log(obj[' Foo Bar ']); // 123

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