Function Expression VS. Fuction Statement

Hi, I’m dealing with Rock, Paper, Scissors Project in Functions, JavaScript.

I’ve already finished it and it works good. However I want to know why the Project works if I use whether Functions Expressions or Funtios Statements and in Which Project wouldn’t work if I choose whichever.

One of the main differences is that function declarations will be hoisted, meaning they are loaded before any code is executed. Imagine that you make a function call in line 2 of your code, but the function declaration is below it. It would still work. Function declarations become available to all your code.

Function expressions on the other hand, are saved as variables or directly defined as arguments to other functions. They aren’t hoisted but that can be a good thing. Function expressions avoid polluting the global scope, and that’s a big deal. Often you’ll find that you need a function to do a very specific job, and the rest of your code doesn’t need it at all. These are prime cases when you’ll want to use function expressions.

some extra reading on the topic

4 Likes

So function statment load before any code, whereas function expression load when the interpreter reaches the line of code.

“Function expressions avoid polluting the global scope, and that’s a big deal. Often you’ll find that you need a function to do a very specific job, and the rest of your code doesn’t need it at all.”

I like it. Thanks!!