Advertisement
karlakmkj

Function expressions - anonymous function

Dec 9th, 2020 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const plantNeedsWater = function (day) {  //define a function inside an expression without a function name
  2.   if (day === "Wednesday"){
  3.     return true;  //inside the function we return the result instead, as we might not want to print eg. addition but we want to print the result of doing addition on variable 1 and 2
  4.   }
  5.   else {
  6.     return false; //usually a function shouldn’t print anything and instead return a value
  7.   }
  8. }
  9.  
  10. console.log(plantNeedsWater('Tuesday')); //hence when we call the function, we will then decide what to do with the value eg. print it
  11.  
  12. /*
  13. For arrow function, remove the function keyword and add in arrow after the parameter
  14. const plantNeedsWater = (day) => {
  15.   if (day === 'Wednesday') {
  16.     return true;
  17.   } else {
  18.     return false;
  19.   }
  20. };
  21.  
  22. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement