Skip to content

SyntaxError: Invalid shorthand property initializer

Quick summary

You wrote { 1+1 } or { foo() } in an object literal expecting shorthand, but shorthand only works with identifiers.

Why this happens

  • Using an expression where a shorthand property name is expected

  • Mixing computed and shorthand syntax incorrectly

  • Forgetting the key in a key-value pair inside an object literal

Minimal example

✗ Broken code
const obj = { 1 + 1 }; // SyntaxError: Invalid shorthand property initializer
✓ Fixed code
const key = 1 + 1;
const obj = { [key]: true }; // { 2: true }

Shorthand { x } only works with a variable name. An expression like 1+1 must be wrapped in computed key syntax [expr]: value.

How to diagnose

  • Read the SyntaxError pointing at the object literal

  • Check each property for valid key:value or shorthand syntax

  • Use computed keys [expr]: value when the key is an expression

How to fix

  • Provide an explicit key: value pair

  • Use computed property syntax [expr]: value for expression keys

  • Ensure shorthand { x } only uses a defined variable x

How to prevent

  • Use a linter that validates object literal syntax

  • Prefer explicit key: value pairs for clarity

Related Resources

Related Glossary

Related Lessons

  • Objects

    Learn about object literals.

← Back to language