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
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
ES6 adds some nice support for easily defining object literals.
Consider the following code:
2019-05-17 06:20:30 -07:00
```js
const getMousePosition = (x, y) => ({
x: x,
y: y
});
```
2019-07-10 10:31:15 +02:00
< code > getMousePosition< / code > is a simple function that returns an object containing two properties.
2018-09-30 23:01:58 +01:00
ES6 provides the syntactic sugar to eliminate the redundancy of having to write < code > x: x< / code > . You can simply write < code > x< / code > once, and it will be converted to< code > x: x< / code > (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 });
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-07-10 10:31:15 +02:00
Use object property shorthand with object literals to create and return an object with < code > name< / code > , < code > age< / code > and < code > gender< / code > properties.
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-07-10 10:31:15 +02:00
- text: '< code > createPerson("Zodiac Hasbro", 56, "male")</ code > should return < code > {name: "Zodiac Hasbro", age: 56, gender: "male"}</ code > .'
testString: assert.deepEqual({name:"Zodiac Hasbro",age:56,gender:"male"}, createPerson("Zodiac Hasbro", 56, "male"));
- text: Your code should not use < code > key:value</ code > .
testString: getUserInput => assert(!getUserInput('index').match(/:/g));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
const createPerson = (name, age, gender) => {
"use strict";
// change code below this line
return {
name: name,
age: age,
gender: gender
};
// change code above this line
};
console.log(createPerson("Zodiac Hasbro", 56, "male")); // returns a proper object
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2018-11-26 16:05:01 +05:30
const createPerson = (name, age, gender) => {
"use strict";
return {
name,
age,
gender
};
};
2018-09-30 23:01:58 +01:00
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
< / section >