함수를 선언하는 방법은 function()
과 화살표 함수
가 있다
function()
input값
)를 처리해 결과를 만들어 낼 수 있음 const a = 2;
const b = 7;
const sum = a + b;
console.log(sum); // 9
👨🏫위 코드를 함수로 만들어보면 아래와 같음
function add(a, b) { // a와 b는 파라미터
return a + b; // 결과값 반환
}
const sum = add(2, 7);
console.log(sum); // 9
return
사용되는 순간 함수 종료됨! function hello(name) {
return `Hello ${name}`;
}
const result = hello('Emma');
console.log(result); // Hello Emma
() => {}
function add(a, b) {
return a + b;
}
// 화살표 함수로 바꾸면
const add = (a, b) => {
return a + b;
}
const sum = add(2, 7);
console.log(sum); // 9
const add = (a, b) => a + b;
const sum = add(2, 7);
console.log(sum); // 9
hello()
함수를 화살표 함수로 바꾸면 const hello = (name) => {
console.log(`Hello ${name}`);
}
hello('Emma');