종우공간 2021. 8. 13. 01:54

예제

const sayHello = function() {
    console.log('Hello');
}

const sayHello = () => {
    console.log('Hello');
}

// One line function does not need curly braces
const sayHello = () => console.log('Hello');

const sayHello = function() {
    return 'Hello';
}

// One line returns
const sayHello = () => 'Hello';

// Return object(need to embrace with parenthesis)
const sayHello = () => ({msg: 'Hello'});

// Single param does not need paranthesis
const sayHello = name => console.log(`Hello ${name}`);

// Multiple params need paranthesis
const sayHello = (firstName, lastName) => console.log(`Hello ${firstName} ${lastName}`);

sayHello('Brad', 'Traversy');

// Array with map
const users = ['Nathan', 'John', 'Williams'];

const nameLength = users.map(function(name) {
    return name.length;
});

// Shorter
const nameLength = users.map((name) => {
    return name.length;
});

// Shortest
const nameLength = users.map(name => name.length);

console.log(nameLength);

- one line function does not need curly braces

- returning object should be embraced with paranthesis

- single parameter does not need paranthesis

- multiple parameters need paranthesis