2018-09-30 23:01:58 +01:00
---
id: 587d7b8a367417b2b2512b4f
2019-07-10 10:31:15 +02:00
title: Write Concise Object Literal Declarations Using Object Property Shorthand
2018-09-30 23:01:58 +01:00
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301225
2021-01-13 03:31:00 +01:00
dashedName: write-concise-object-literal-declarations-using-object-property-shorthand
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
ES6 adds some nice support for easily defining object literals.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
Consider the following code:
2019-05-17 06:20:30 -07:00
```js
const getMousePosition = (x, y) => ({
x: x,
y: y
});
```
2020-11-27 19:02:05 +01:00
`getMousePosition` is a simple function that returns an object containing two properties. ES6 provides the syntactic sugar to eliminate the redundancy of having to write `x: x` . You can simply write `x` once, and it will be converted to`x: x` (or something equivalent) under the hood. Here is the same function from above rewritten to use this new syntax:
2019-05-17 06:20:30 -07:00
```js
const getMousePosition = (x, y) => ({ x, y });
```
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Use object property shorthand with object literals to create and return an object with `name` , `age` and `gender` properties.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`createPerson("Zodiac Hasbro", 56, "male")` should return `{name: "Zodiac Hasbro", age: 56, gender: "male"}` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
{ name: 'Zodiac Hasbro', age: 56, gender: 'male' },
createPerson('Zodiac Hasbro', 56, 'male')
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
Your code should not use `key:value` .
```js
(getUserInput) => assert(!getUserInput('index').match(/:/g));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
const createPerson = (name, age, gender) => {
2020-03-04 13:08:54 -06:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
return {
name: name,
age: age,
gender: gender
};
2020-03-04 13:08:54 -06:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
};
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2018-11-26 16:05:01 +05:30
const createPerson = (name, age, gender) => {
return {
name,
age,
gender
};
};
2018-09-30 23:01:58 +01:00
```