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