FAQ: Functions - Function Expressions

This community-built FAQ covers the “Function Expressions” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Function Expressions

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

4 posts were split to a new topic: Can you change parameters on constant functions?

2 posts were split to a new topic: Why do I need to do declare a variable?

3 posts were split to a new topic: What are the advantages of function expressions?

8 posts were split to a new topic: Why do I need to call the function vs logging it?

2 posts were split to a new topic: How do I use return?

2 posts were split to a new topic: Advanced functions

2 posts were split to a new topic: Javascript if/else shorthand

5 posts were merged into an existing topic: Why do I need to call the function vs logging it?

2 posts were split to a new topic: Help solving my code?

2 posts were split to a new topic: Can a variable represent multiple anonymous functions?

3 posts were split to a new topic: Ternary operator troubleshooting

2 posts were split to a new topic: Console logging function name?

Hello;
As asked in the excercise, I wrote this :

const plantNeedsWater = function(day) {
  if (day === "Wednesday") {
    return true;
  } else {
    return false;
  }
}

plantNeedsWater("Tuesday");
console.log(plantNeedsWater()); // prints false as expected

And it returns false as expected.
But I tried to change the code by calling plantNeedsWater with “Wednesday” parameter and the code still logs “false”; thus my function doesn’t properly work as far as I understand it (very badly at the moment apparently)

const plantNeedsWater = function(day) {
  if (day === "Wednesday") {
    return true;
  } else {
    return false;
  }
}

plantNeedsWater("Wednesday");
console.log(plantNeedsWater());
// prints false ???

Thanks in advance for your answer

why do you call the function twice?

when you call the function here:

console.log(plantNeedsWater());

you provide no argument, thus the parameter (day) gets a value of undefined.

In the first call:

plantNeedsWater("Wednesday");

you don’t log the returned result

in my opinion, this is a flaw in the language, a missing argument for a required parameter should just raise an error or throw an exception.

3 Likes

const plantNeedsWater=function(day){
if (day === ‘Wednesday’) {
return true
} else {
return false;
console.log(No need to water on ${day}s.);
}
}

why doesn’t it log the message into the console?

return hands back data to the caller, and then function is done. So the log statement should be before return

Can I use the var keyword to declare a function expression? Why use a const keyword?

one simple way to find out: Try it.

const is block-scoped, and it prevents you from actually overwriting the function/variable later in your program

let and const where introduced in es6, i would recommend using them unless you have a good reason to use var

I am wondering if there is any benefit to writing an if-else statement. The else is not necessary in this function’s case considering the code ends as soon as it runs a return statement. The following two codes function the same way:

const plantNeedsWater = function(day){
  if(day === 'Wednesday') {
    return true;
  }
    return false;
}
const plantNeedsWater = function(day){
  if(day === 'Wednesday') {
    return true;
  } else {
    return false;
}
}

Is the second better for readability? Does one of them run faster?