2018-09-30 22:01:58 +00:00
---
id: 587d7b87367417b2b2512b43
title: Use Arrow Functions to Write Concise Anonymous Functions
challengeType: 1
2019-08-05 16:17:33 +00:00
forumTopicId: 301211
2021-01-13 02:31:00 +00:00
dashedName: use-arrow-functions-to-write-concise-anonymous-functions
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2018-09-30 22:01:58 +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-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
To achieve this, we often use the following syntax:
2019-05-17 13:20:30 +00:00
```js
const myFunc = function() {
const myVar = "value";
return myVar;
}
```
2020-11-27 18:02:05 +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** :
2019-05-17 13:20:30 +00:00
```js
const myFunc = () => {
const myVar = "value";
return myVar;
}
```
2020-11-27 18:02:05 +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:
2019-05-17 13:20:30 +00:00
```js
2019-10-22 00:50:51 +00:00
const myFunc = () => "value";
2019-05-17 13:20:30 +00:00
```
2020-11-27 18:02:05 +00:00
This code will still return the string `value` by default.
# --instructions--
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` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2021-03-03 00:12:12 +00:00
You should replace the `var` keyword.
2020-11-27 18:02:05 +00:00
```js
(getUserInput) => assert(!getUserInput('index').match(/var/g));
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`magic` should be a constant variable (by using `const` ).
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
(getUserInput) => assert(getUserInput('index').match(/const\s+magic/g));
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`magic` should be a `function` .
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
assert(typeof magic === 'function');
2018-09-30 22:01:58 +00:00
```
2021-03-03 00:12:12 +00:00
`magic()` should return the correct date.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(magic().setHours(0, 0, 0, 0) === new Date().setHours(0, 0, 0, 0));
```
2021-03-03 00:12:12 +00:00
The `function` keyword should not be used.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
(getUserInput) => assert(!getUserInput('index').match(/function/g));
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
var magic = function() {
2018-10-13 11:55:03 +00:00
return new Date();
};
2018-09-30 22:01:58 +00:00
```
2019-07-18 15:24:12 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
```js
const magic = () => {
return new Date();
};
```