FAQ: Functions - Function Expressions

Hi

I’ve just done the function expression exercise and trying to get to grips with how this is different from function declarations. The below code gives a true/false answer

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

plantNeedsWater(‘Tuesday’)

console.log(plantNeedsWater(‘Tuesday’))

I was trying to write it out as a function declaration however I keep receiving errors. The only thing I can think of is that the plantNeedsWater isn’t the function and I need to define a new one perhaps?

function plantNeedsWater(day) {
if (day === ‘Wednesday’) {
return true;
} else {
return false;
};
const day = ‘Wednesday’
console.log(plantNeedsWater)


what does the result mean here! usually we log like console.log(plantNeedsWater(‘Tuesday’); this. Why can’t i log like this way?Please help me out!

Remember to invoke the function else all you log is the reference, as evidenced above.

plantNeedsWater()
1 Like

Why is lesson 6 did we not use the if else?

To which lesson are you referring? Please post a link to the exercise page.

sure: https://www.codecademy.com/courses/introduction-to-javascript/lessons/functions/exercises/return

1 Like

Not every function requires decision logic. This exercise is only doing arithmetic… multiplying two numbers and returning the product.

got it I understand your point. Logic vs arithmetic function makes the difference between these using if else

1 Like

Why should I use a function expression instead of simply using functions?

For one, function expressions can be protected with the const declaration keyword. Function declarations can be deleted or overwritten.

Ultimately, go with the form you like but keep it consistent throughout. All function declarations, or no function declarations (unless it is important to have the function hoisted, but that is another question).

1 Like

Hi,
This may be more of an observation than a question but maybe there is some other nuance here. The lesson describes “function expression” as an “anonymous function” i.e., no name yet, it goes on to say that “it is often stored in a variable in order to refer to it”. - therefore … it does have a name, the name of the variable. I’m sure there is an important distinction - can you delve into that a little?

The variable has a name. The function is still anonymous (written as an expression). The variable can be reassigned unless we declared it with const.

The principle difference is in which functions can be hoisted. Named function declarations can be; anonymous functions cannot. If we follow the current line of thinking and use function expressions in our code, then the hoisting question is rather moot since it will not apply.

Technically we do not store anything in a variable. The variable is simply a label that is associated with an object stored somewhere in memory (the OS and interpreter know where).


There is one rabbit hole you may yet venture into, that is, named function expressions. We won’t go there, today and suggest leave this until way later to explore. If it has any benefits they are obscure and suited to a particular scenario, one which we never cover here.

Roy, thanks, that is exactly the kind of additional nuance and clarity I was hoping for!

1 Like

if(day ===‘Wednesday’){
return true;
} what happen if this one return to true ?