Resolve SyntaxError: Invalid shorthand property initializer in JavaScript

SyntaxError: Invalid shorthand property initializer error comes when we use the equals operator rather than a colon to separate key-values(properties) in the object.

To resolve this issue, we need to use a colon(:) rather than the equals operator(=) between the key and values of an object.

Let’s see with the help of an example:

const country = {
  name = 'Bhutan', //  need to use : and not =
  population = 3000,
}

Notice that we have used = between name and Bhutan. This causes the syntax error.

To resolve this issue, use a colon between the key-value pair of an object.

const country = {
  name : 'Bhutan', //  Used : correctly
  population : 3000,
};
console.log(country);

Leave a comment

Your email address will not be published. Required fields are marked *