let text = 'Hello World!';
const secret = 'my_secret!@#';
Template literals
let num = 55;
`This is value of num variable: ${num}.`;
`This string uses backticks.`;
`Current time ${Date.now()}.`;
// "Current time Fri Apr 20 2018 22:54:48 GMT+0400 (+04)."
const arr = [4, 5, 6];
for (let v of arr) {
console.log(v);
}
// 4, 5, 6
Functions
// just function
function getDate() {
return new Date();
}
// arrow function
const multiply = n => n**2;
setTimeout(() => {
console.log('Hello');
}, 1000);
// *Arrow Functions lexically bind their context
// so this actually refers to the originating context.
Function Generator
// generator function*
function* gen() {
let n = yield 123;
return n + 2;
}
// push value into generator function*
function* gen() {
console.log(11);
let n = yield 1;
console.log(n);
return 2;
}
Callback
function asyncFun(cb) {
// ...
cb();
}
asyncFun((err) => {
if (err) {
console.error(err);
return;
}
console.log('cb called after 1000 ms');
});