normal function
function sayHello(firstName, lastName) {
return `hello ${firstName} ${lastName}`;
}
function expression
const sayHello2 = function(firstName, lastName) {
return `hello ${firstName} ${lastName}`;
};
arrow function
const sayHello3 = (firstName, lastName) => `hello ${firstName} ${lastName}`;
if you want to return an object in arrow function, we could do something like below
const makeAToy = (color, size) => ({ toyColor: color, toySize: size });
Immediately Invoked Function Expression
the function will be executed immediately
(function(firstName, lastName) {
console.info(`hello ${firstName} ${lastName}`);
})('Amy', 'Li');
methods in object
const greeting = {
word: 'greeting',
sayHello1(firstName, lastName) {
console.log(`${this.word} ${firstName} ${lastName}`);
},
sayHello2(firstName, lastName) {
console.log(`${this.word} ${firstName} ${lastName}`);
},
sayHello3: (firstName, lastName) => {
console.log(`${greeting.word} ${firstName} ${lastName}`);
},
};
call back function
setTimeout(() => console.log('time to do something'), 1000);