2020-08-04 07:13:35 +00:00
---
id: 587d7b8a367417b2b2512b4f
2021-02-06 04:42:36 +00:00
title: Write Concise Object Literal Declarations Using Object Property Shorthand
2020-08-04 07:13:35 +00:00
challengeType: 1
forumTopicId: 301225
2021-01-13 02:31:00 +00:00
dashedName: write-concise-object-literal-declarations-using-object-property-shorthand
2020-08-04 07:13:35 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
ES6 adds some nice support for easily defining object literals.
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
Consider the following code:
2020-08-04 07:13:35 +00:00
```js
const getMousePosition = (x, y) => ({
x: x,
y: y
});
```
2021-02-06 04:42:36 +00: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:
2020-08-04 07:13:35 +00:00
```js
const getMousePosition = (x, y) => ({ x, y });
```
2020-12-16 07:37:30 +00:00
# --instructions--
2020-08-04 07:13:35 +00:00
2021-02-06 04:42:36 +00:00
Use object property shorthand with object literals to create and return an object with `name` , `age` and `gender` properties.
2020-08-04 07:13:35 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2020-08-04 07:13:35 +00:00
2021-02-06 04:42:36 +00:00
`createPerson("Zodiac Hasbro", 56, "male")` should return `{name: "Zodiac Hasbro", age: 56, gender: "male"}` .
2020-08-04 07:13:35 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.deepEqual(
{ name: 'Zodiac Hasbro', age: 56, gender: 'male' },
createPerson('Zodiac Hasbro', 56, 'male')
);
2020-08-04 07:13:35 +00:00
```
2021-02-06 04:42:36 +00:00
Your code should not use `key:value` .
2020-08-04 07:13:35 +00:00
```js
2020-12-16 07:37:30 +00:00
(getUserInput) => assert(!getUserInput('index').match(/:/g));
2020-08-04 07:13:35 +00:00
```
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```js
const createPerson = (name, age, gender) => {
// Only change code below this line
return {
name: name,
age: age,
gender: gender
};
// Only change code above this line
};
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```js
const createPerson = (name, age, gender) => {
return {
name,
age,
gender
};
};
```