const myFunc = function() {ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use arrow function syntax:
const myVar = "value";
return myVar;
}
const myFunc = () => {When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword
const myVar = "value";
return myVar;
}
return
as well as the brackets surrounding the code. This helps simplify smaller functions into one-line statements:
const myFunc = () => "value"This code will still return
value
by default.
magic
which returns a new Date()
to use arrow function syntax. Also make sure nothing is defined using the keyword var
.
var
keyword.
testString: getUserInput => assert(!getUserInput('index').match(/var/g));
- text: magic
should be a constant variable (by using const
).
testString: getUserInput => assert(getUserInput('index').match(/const\s+magic/g));
- text: magic
should be a function
.
testString: assert(typeof magic === 'function');
- text: magic()
should return correct date.
testString: assert(magic().setHours(0,0,0,0) === new Date().setHours(0,0,0,0));
- text: function
keyword should not be used.
testString: getUserInput => assert(!getUserInput('index').match(/function/g));
```