function add(a = 2, b = 4) {
console.log(`${a} + ${b}`);
return a + b;
}
console.log(
add(),
add(1),
add(1, 3)
);
arguments
: 함수 내에서 사용가능한 지역 변수
function add(a, b) {
console.log('1.', arguments); // 1. [1,3,5,7]
console.log('2.', arguments[0]); // 2. 1
console.log('3.', typeof arguments); // 3. object
return a + b;
}
console.log(
'4.', add(1, 3, 5, 7) // 4. 4
);
function add(a, b) {
for (const arg of arguments) {
console.log(arg);
}
return a + b;
}
console.log(
add(1, 3, 5, 7)
);
for ~ of 문을 사용할 수 있음.
화살표 함수에서는 arguments
사용 불가능함.