FAQ: Functions - Function Expressions

I’m a little confused as to the application for Function Expressions, at least in the way it’s being shown in the exercise and my current understanding.

At a glance it appears redundant to declare a variable to hold a function when a function can just be declared on it’s own. Is it maybe related to memory usage throughout a larger program?

I think an example of a practical use case would set me straight.

Insight would be appreciated.
Thanks!

This is an article from 2015, before arrow functions were adopted into regular use, but it can apply to them, as well, for the most part.

Quick Tip: Function Expressions vs Function Declarations

Conslusion
As we’ve seen, function expressions don’t offer much that can’t be achieved with function declarations, but using them can often result in cleaner and more readable code. Their widespread use makes them an essential part of every developer’s toolbox.

2 Likes

I tried to turn this (working) code:

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

Into this in order to reduce what is needed

const plantNeedsWater = function(day) {
day === ‘Wednesday’ ? true: false;
}
console.log(plantNeedsWater(‘Wednesday’));

However, when using the second code I get ‘undefined’ printed rather than ‘true’. What is this happening and is there a way to get the second code to behave like the first one?

Edit: Found solution in a example a few exercises later (after being too stubborn to move on at first)

return day === ‘Wednesday’ ? true: false;

fixes the problem

1 Like

A comparison results in Boolean value, so you could even do:

return day === 'Wednesday'

Hey everyone! I’m a bit confused, why do I need to call a function right before logging the result to the console:

const plantNeedsWater = function(day) {
  if(day === 'Wednesday') {
    return true;
  } else {
    return false;
  }
}
plantNeedsWater('Wednesday'); // here
console.log(plantNeedsWater('Wednesday'))

If I omit that step I will get the same result in the console as well. It seems that only the last value of the argument, which we have passed in here “console.log(plantNeedsWater(‘Wednesday’))”, matters. For example, if I assinged any other value there in the console.log step - I would get ‘false’, no matter what value we have assinged to the argument before. Thank you in advance.

who says you need to do this?

I was just doing everything according to task, one of the steps was to call the function and assign an argument. So as far as I understood there is no practical need to do this?

calling plantNeedsWater function and logging the result is certainly useful, not sure why you call the first without logging the result. Its not needed

Thank you. I might have misunderstood something.

QUESTION ONE. Create a variable named plantNeedsWater using the const variable keyword.
MY ANSWER ONE. const plantNeedsWater = function();{}
Is this right and if not what is the answer.

QUESTION TWO. Assign an anonymous function that takes in a parameter of day to plantNeedsWater.
MY ANSWER TWO. const plantNeedsWater = function (day);{}

I’m stuck, can’t figure this out. Need please to see the answer for each part separately. Thanks!

Never would we write a semi-colon before a code block.

function () {

}

can you define a function expression with multiple anonymous functions stored (perhaps attached by an operator) in one variable? if so, how do you pass arguments to the functions?

Function expressions can be cached in the same way any other values can. We can have an array filled with nothing but function expressions. An object can have a unlimited number of methods which are essentially function expressions but tied to the object.

In other words, any thing that can be referenced can hold functions.

This will be a bit of silliness but let’s say we have an array of function expressions.

let func = [
  x => x * 5,
  x => x / 5,
  x => x + 5,
  x => x - 5,
  x => x ** 0.5
]

Now let’s say we have the user input one of these operators, and in another input, a number.

let op = prompt("* | / | + | - | r");
let num = prompt("Enter a number:");

Now using the information provided we can invoke a function on the value they entered.

console.log(func['*/+-r'.indexOf(op)](num))

We can of course refine this to include validation, but the gist is basically what we see here.


Guess I better go off and test this to make sure it works. Above is theoretical, but should work.

 > op = prompt("* | / | + | - | r")
<- "r"
 > num = prompt("Enter a number:")
<- "25"
 > func['*/+-r'.indexOf(op)](num)
<- 5

Expected result? Check.


In all the thousands of posts I’ve written there are a handful of examples similar to this one that were essentially invented on the fly like above. This question does come up periodically and my response would likely be seat of the pants. I’m more like a cloud than a fountain.

Here is one written in Python that actually deals with roughly the same idea in process, if not in expression. Python has lambdas that could be packaged the same way we did with the arrow functions above, but in this example I went with enclosed definitions in the function.

Don’t try too hard to understand the code, as much as the illustration. You will see parallels with the above idea.

My if statement isnt working