We've just learned a bunch of ES6.
We've learned:
Remember, When declaring variables using ES6 syntax, if you need to declare a variable whose value will not change, declare it as a const
. If the variable's value will or might change, unless you need a global scope, you'll likely declare it as a let
.
const addTwo = num => {
return num + 2;
}
const addTwo = num => num + 2;
function eatBreakfast(pancakes) {
var that = this;
that.food = 'Knife please?';
Waiter.bringCutlery(function (silverware) {
that.food = silverware;
});
}
is now
const eatBreakfast = pancakes => {
this.food = 'Knife please?';
Waiter.bringCutlery((silverware) => this.food = silverware);
}
The second piece of code is synonymous with the first piece of code. The second piece of code saves you from writing price: price
when you're declaring an object key-value pair inside an object.
const price = 100;
const item = {price: price};
const price = 100;
const item = {price};
Remember, these require you to use the tick
``` quotation mark. The single quote or double-quote marks won't invoke string interpolation.
const greeting = name => `Hi, ${name}.`;