function getReminder(parameter) {
console.log('Water the plants');
}
function sayThanks(){
console.log('Thank you for your purchase! We appreciate your business');
}
sayThanks();
//Output: Thank you for your purchase! We appreciate your business
function sayThanks(name) {
console.log('Thank you for your purchase '+ name +'! We appreciate your business.');
}
sayThanks('Cole');
function makeShoppingList( item1 = 'milk', item2 = 'bread', item3 = 'eggs')
{
console.log(`Remember to buy ${item1}`);
console.log(`Remember to buy ${item2}`);
console.log(`Remember to buy ${item3}`);
}
makeShoppingList();
/*Output
Remember to buy milk
Remember to buy bread
Remember to buy eggs
*/
function makeShoppingList( item1 = 'foods', item2 = 'foods', item3 = 'foods')
{
console.log(`Remember to buy ${item1}`);
console.log(`Remember to buy ${item2}`);
console.log(`Remember to buy ${item3}`);
}
makeShoppingList('milk', 'eggs', 'bread');
/*Output
Remember to buy milk
Remember to buy eggs
Remember to buy bread
*/
함수를 실행을 종료하고 주어진 값을 호출했던 지점으로 반환
function monitorCount(rows, columns){
return rows*columns;
}
const numOfMonitors = monitorCount(5,4);
console.log(numOfMonitors);
undefined로 나오는 이유?
Return은 언제 쓰는가?
function monitorCount(rows, columns) {
return rows * columns;
};
function costOfMonitors(rows, columns){
return monitorCount(rows, columns)* 200;
};
const totalCost = costOfMonitors(5,4);
console.log(totalCost);
//Output : 4000
const plantNeedsWater = function (day) {
if(day === 'Wednesday') {
return true;
} else{
return false;
}
};
plantNeedsWater('Tuesday');
console.log(plantNeedsWater('Tuesday'));
//Output: false
키워드 함수를 입력하지 않고도 화살표를 통해 함수 입력이 가능
단 parameter는 반드시 있어야 한다!
const plantNeedsWater = function(day) {
if (day === 'Wednesday') {
return true;
} else {
return false;
}
};
//밑의 코드로 변환이 가능!
const plantNeedsWater = (day) => {
if (day === 'Wednesday') {
return true;
} else {
return false;
}
};
Zero Parameters
const functionName = () => {};
One Parameter
const functionName = paraOne => {};
Two or more Parameters
const functionName = (paraOne, paraTwo) => {};
1)
const squareNum = (num) => {
return num * num;
};
//다음과 같이 변환 가능하다
const squareNum = num => num * num;
2)
const plantNeedsWater = (day) => {
return day === 'Wednesday' ? true : false;
};
//다음과 같이 변환 가능하다
const plantNeedsWater = day =>
day === 'Wednesday' ? true : false;