// ch20 Function
function calcilateAge(birthYear){// you can also pass several var here
return 2018-birthYear;
}
var ageJohn=calcilateAge(1990);// you just CALL function
var ageMike=calcilateAge(1948);
var ageJane=calcilateAge(1989);
console.log(ageJohn, ageMike, ageJane);// console printed // 28
function yearsUntilRetirement(year, firstName){
var age = calcilateAge(year);
var retire = 65 - age;
if(retire>0){
console.log(firstName+" retires in "+ retire+" years." );
}else{
console.log(firstName+ " is already retired.");
}
}
yearsUntilRetirement(1990, "John"); // console printed // John retires in 37 years.
yearsUntilRetirement(1948, "Mike"); //Mike is already retired.
yearsUntilRetirement(1989, "Jane");
// ch21 Function Statements and Expressions
- function declairation
ex) function whatDoYouDo(job,firstName){}
- function expression
ex)
2+3, typeof...
-> they bring out immediate results.
var whatDoYouDo = function (job, firstName){
switch (job){
case "teacher":
return firstName +" teaches kids how to code.";
case "driver":
return firstName +" drives a taxi.";
case "designer":
return firstName +" designs amazing clothes.";
default:
return firstName+" does nothing."
}
}
console.log(whatDoYouDo("teacher", "John"));
console.log(whatDoYouDo("designer", "Jane"));
console.log(whatDoYouDo("none", "Jakob"));
- function statements: they don't bring out the result immediately.
'javascript' 카테고리의 다른 글
JavaScript/ Loops and Iteration (0) | 2020.02.13 |
---|---|
JavaScript/ Objects and Methods (0) | 2020.02.13 |
JavaScript/ Object and Properties (0) | 2020.02.13 |
JavaScript/ Array Basic (0) | 2020.02.13 |
JavaScript/ Basic (0) | 2020.02.13 |