2018-10-10 18:03:03 -04:00
---
id: 587d7b87367417b2b2512b43
2021-02-06 04:42:36 +00:00
title: Use Arrow Functions to Write Concise Anonymous Functions
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:13:35 +08:00
forumTopicId: 301211
2021-01-13 03:31:00 +01:00
dashedName: use-arrow-functions-to-write-concise-anonymous-functions
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
In JavaScript, we often don't need to name our functions, especially when passing a function as an argument to another function. Instead, we create inline functions. We don't need to name these functions because we do not reuse them anywhere else.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
To achieve this, we often use the following syntax:
2020-08-04 15:13:35 +08:00
```js
const myFunc = function() {
const myVar = "value";
return myVar;
}
```
2021-02-06 04:42:36 +00:00
ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use **arrow function syntax** :
2020-08-04 15:13:35 +08:00
```js
const myFunc = () => {
const myVar = "value";
return myVar;
}
```
2021-02-06 04:42:36 +00:00
When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword `return` as well as the brackets surrounding the code. This helps simplify smaller functions into one-line statements:
2020-08-04 15:13:35 +08:00
```js
const myFunc = () => "value";
```
2021-02-06 04:42:36 +00:00
This code will still return the string `value` by default.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2021-02-06 04:42:36 +00:00
Rewrite the function assigned to the variable `magic` which returns a `new Date()` to use arrow function syntax. Also, make sure nothing is defined using the keyword `var` .
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
User should replace `var` keyword.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
(getUserInput) => assert(!getUserInput('index').match(/var/g));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`magic` should be a constant variable (by using `const` ).
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
(getUserInput) => assert(getUserInput('index').match(/const\s+magic/g));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`magic` should be a `function` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(typeof magic === 'function');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`magic()` should return correct date.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(magic().setHours(0, 0, 0, 0) === new Date().setHours(0, 0, 0, 0));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`function` keyword should not be used.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
(getUserInput) => assert(!getUserInput('index').match(/function/g));
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:13:35 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
var magic = function() {
return new Date();
};
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
const magic = () => {
return new Date();
};
```