Sometimes, we may run into the ‘SyntaxError: missing : after property id’ when we’re developing JavaScript apps.
In this article, we’ll look at how to fix the ‘SyntaxError: missing : after property id’ when we’re developing JavaScript apps.
Fix the ‘SyntaxError: missing : after property id’ When Developing JavaScript Apps
To fix the ‘SyntaxError: missing : after property id’ when we’re developing JavaScript apps, we should make sure we have a : separating object property key and value in our object literals.
On Edge, the error message for this error is SyntaxError: Expected ':'.
And on Firefox, the error message for this error is SyntaxError: missing : after property id.
For instance, the following code will throw the error:
const obj = { propertyKey = 'value' };
since we have an = between propertyKey and 'value'.
To fix this, we write:
const obj = { propertyKey: 'value' };
We can also use the square bracket notation to add properties to objects:
So we can write:
const obj = { };
obj['propertyKey'] = 'value';
We can also write:
const obj = { ['b'+'ar']: 'foo' };
to add the bar property to obj with the square bracket notation.
The square bracket takes any expression that returns a string.
Conclusion
To fix the ‘SyntaxError: missing : after property id’ when we’re developing JavaScript apps, we should make sure we have a : separating object property key and value in our object literals.
On Edge, the error message for this error is SyntaxError: Expected ':'.
And on Firefox, the error message for this error is SyntaxError: missing : after property id.