Course contentsShow
← JavaScript
Lesson 621 of 2,72315. ObjectsFree lesson

Combining Shorthand and Computed Keys

Mix property shorthand, method shorthand, and computed keys in a single object literal.

Combining Shorthand and Computed Keys

You'll learn how to use property shorthand, method shorthand, and computed property names together in one object literal for maximum expressiveness.

The Power of Mixing Syntax Features

Now that you've learned property shorthand, method shorthand, and computed keys separately, here's the good news: you can combine them all in a single object literal. JavaScript doesn't force you to choose just one style—mix and match based on what each property needs.

Think of it like building with different types of LEGO blocks. Some pieces are standard bricks (regular properties), some are shortcuts (shorthand), and some need custom shapes (computed keys). You use whatever fits best for each spot.

Practical Example

const prefix = "user";
const name = "Alice";
const age = 30;

const user = {
  // Property shorthand
  name,
  age,
  
  // Computed key with a variable
  [prefix + "ID"]: 12345,
  
  // Computed key with template literal
  [`${prefix}Status`]: "active",
  
  // Method shorthand
  greet() {
    return `Hello, I'm ${this.name}`;
  },
  
  // Computed method name
  [`get${prefix.charAt(0).toUpperCase() + prefix.slice(1)}Info`]() {
    return `${this.name}, ${this.age}`;
  }
};

console.log(user.name);           // "Alice"
console.log(user.userID);         // 12345
console.log(user.userStatus);     // "active"
console.log(user.greet());        // "Hello, I'm Alice"
console.log(user.getUserInfo());  // "Alice, 30"

Notice how you can freely combine different syntaxes based on your needs—no property is "jealous" of another's style!

Key Takeaway: You can mix property shorthand, method shorthand, and computed keys in any combination within a single object literal, using the right tool for each property's requirements.