Object Property Shorthand in JavaScript

Level 3136 keystrokes

ES6 shorthand allows omitting the value when property name matches variable name.

JavaScript code

// These variable names will be reused as property names
1const name = "Alice";
2const age = 25;
// Shorthand: { name, age } is the same as { name: name, age: age }
// greet() { ... } is shorthand for greet: function() { ... }
3const person = { name, age, greet() { return `Hi, ${this.name}`; } };
// this.name is "Alice", so this prints "Hi, Alice"
4console.log(person.greet());

Output

Hi, Alice
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • ;× 5
  • ,× 3
  • .× 3
  • (× 3
  • )× 3
  • {× 3
  • }× 3
  • =× 3
  • "× 2
  • `× 2

More JavaScript examples at level 3

← JavaScript typing practice