Generally I prefer the declarative syntax because it's how it's done in most languages, and the less work I have to make for myself writing code in a different style, and the less work other people make for me reading in another style, the better. If you're only working in JS and only plan to ever work in JS, you can do whichever you like, but other multi-lingual devs who have to work on your code one day will thank you for not using language specific features if there isn't a clear advantage to doing so when more generic/portable features exist. Answer from alzee76 on google.com
🌐
Google
google.com › goto
Function Declaration vs Function Expression
April 26, 2023 - As you see here, we have the function keyword without a name for the function. This makes it an expression, which you have to assign to a variable (as we have done to generateIntro here). Note: you can use const, let or var to declare the variable.
🌐
Google
google.com › javascript › difference-between-function-declaration-and-function-expression-in-javascript
Difference between ‘function declaration’ and ‘function expression' in JavaScript - GeeksforGeeks
January 3, 2024 - In this article, we will learn ... function and the most prominent difference is that the function declaration has a function name while the latter doesn't have one....
Discussions

Function Declaration vs Expression
You need function declarations if you want to follow the pattern where you put helper functions at the end of a file but call them earlier in the file. That’s sort of an antiquated non-JS pattern at this point though. Lately, I have gravitated towards declarations for top-level functions, but arrow expressions for in-line callbacks and such. It’s all just style choices though. More on reddit.com
🌐 r/learnjavascript
9
4
October 21, 2023
Should I be using function declaration or function expressions?
Generally I prefer the declarative syntax because it's how it's done in most languages, and the less work I have to make for myself writing code in a different style, and the less work other people make for me reading in another style, the better. If you're only working in JS and only plan to ever work in JS, you can do whichever you like, but other multi-lingual devs who have to work on your code one day will thank you for not using language specific features if there isn't a clear advantage to doing so when more generic/portable features exist. More on google.com
🌐 google.com
7
4
March 7, 2022
function declarations vs function expressions
jason J is having issues with: I am so confused Seems hard for me to know when to use one or the other and what either mean?? I need some simple programs in both to see wh... More on teamtreehouse.com
🌐 teamtreehouse.com
1
November 22, 2020
Advantages to use function expression instead of function declaration?
As seen here, there are some differences between function declaration and function expression. A function expression has one disadvantage vs a function declaration, if called before its been decl... More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › function
function expression - JavaScript | MDN
A function expression is very similar to, and has almost the same syntax as, a function declaration. The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions.
🌐
Reddit
reddit.com › r/learnjavascript › function declaration vs expression
r/learnjavascript on Reddit: Function Declaration vs Expression
October 21, 2023 -

So I understand the difference between the two, but is it a case where I should've use a certain one instead of the other ? Or could I just use function expression for everything?

Top answer
1 of 5
5
You need function declarations if you want to follow the pattern where you put helper functions at the end of a file but call them earlier in the file. That’s sort of an antiquated non-JS pattern at this point though. Lately, I have gravitated towards declarations for top-level functions, but arrow expressions for in-line callbacks and such. It’s all just style choices though.
2 of 5
2
Any time you use a function as you would a variable, e.g. assigning it to another variable, passing it as a callback, returning it from a parent function, using the IIFE pattern, these are all function expressions. The only time it's a function declaration is when you begin a statement with the function keyword. Function expressions: setTimeout(function () {}); (function () {})(); const f = function () {}; Function declaration: function f() {} There are a couple of minor differences: assigning a function to a const means it can't be redeclared later, so it can act as a safeguard in that context. This has never been a problem for me in the real world, however. The one big difference is that you can't use an arrow function as a function declaration, only as a function expression. My personal opinion is that, unless you have good reason to deviate, if you're simply... well... declaring a function, use a function declaration. For me, seeing the function keyword at the start of the line makes it clear that you're simply defining a function and not trying to do anything else with it, like immediately invoking it. Many people seem to prefer using const f = ..., presumably because it allows them to use arrow notation, and some people really like arrow notation.
Top answer
1 of 2
5
Generally I prefer the declarative syntax because it's how it's done in most languages, and the less work I have to make for myself writing code in a different style, and the less work other people make for me reading in another style, the better. If you're only working in JS and only plan to ever work in JS, you can do whichever you like, but other multi-lingual devs who have to work on your code one day will thank you for not using language specific features if there isn't a clear advantage to doing so when more generic/portable features exist.
2 of 2
3
It's largely down to preference. Notably, the expression syntax would also get hoisted if you used var instead of const (but would get initialised as undefined). But that's precisely why a lot of devs are now moving over to defining them as expressions with const - because what's actually happening, and what functions are available during each stage of code execution, is a lot clearer. Hoisting is useful. But it's also a sort of "hidden" feature of JS that isn't clear to everyone. Especially as the way var and function hoisting is done is also different. So what it boils down to for me is that function doSomething() {} is sort of equivalent to var doSomething = () => {} (in that both involve hoisting, which is done in different ways, so it isn't immediately clear by just looking at the code). And ultimately, if I see someone using var when I review their code, I tell them to use const instead. Edit: added some clarifications
Find elsewhere
Top answer
1 of 5
430

They're actually really similar. How you call them is exactly the same.The difference lies in how the browser loads them into the execution context.

Function declarations load before any code is executed.

Function expressions load only when the interpreter reaches that line of code.

So if you try to call a function expression before it's loaded, you'll get an error! If you call a function declaration instead, it'll always work, because no code can be called until all declarations are loaded.

Example: Function Expression

alert(foo()); // ERROR! foo wasn't loaded yet
var foo = function() { return 5; } 

Example: Function Declaration

alert(foo()); // Alerts 5. Declarations are loaded before any code can run.
function foo() { return 5; } 


As for the second part of your question:

var foo = function foo() { return 5; } is really the same as the other two. It's just that this line of code used to cause an error in safari, though it no longer does.

2 of 5
114

Function Declaration

function foo() { ... }

Because of function hoisting, the function declared this way can be called both after and before the definition.

Function Expression

  1. Named Function Expression

    var foo = function bar() { ... }
    
  2. Anonymous Function Expression

    var foo = function() { ... }
    

foo() can be called only after creation.

Immediately-Invoked Function Expression (IIFE)

(function() { ... }());

Conclusion

Douglas Crockford recommends to use function expression in his «JavaScript: The Good Parts» book because it makes it clear that foo is a variable containing a function value.

Well, personally, I prefer to use Declaration unless there is a reason for Expression.

🌐
Codecademy
codecademy.com › forum_questions › 5571f657e39efeac7e000032
Difference b/w Function Declaration and Function Expression | Codecademy
There is no fundamental difference in how declared functions or function expressions work. Either form is valid.
🌐
Medium
medium.com › @saiemsaeed › function-declarations-vs-function-expressions-summed-up-22d4f38e90bc
Function Declarations vs Function Expressions — Summed Up | by Saiem Saeed | Medium
June 24, 2020 - Function Declarations vs Function Expressions — Summed Up What is Function Declaration? When a function is defined as a statement that exists as a standalone unit of code and not assigned to a …
🌐
W3Schools
w3schools.com › js › js_function_definition.asp
W3Schools.com
You can call a function before or after it is declared in the code. ... A function expression stores a function inside a variable.
🌐
Google
google.com › goto
Function expressions vs. function declarations revisisted | Go Make Things
October 12, 2021 - Function declarations are more common, and I like to adhere a bit more to conventions. I think it’s more clear that the thing that’s coming is a function because function is the first word you see. My original decision to use function expressions was made early in my career and became a strongly held but loosely supported opinion.
🌐
Google
google.com › goto
Function declaration vs. function expression in JavaScript: When to use them. | by Codevet | Masteringbackend
July 23, 2022 - ... The major difference is a name difference, a function declaration is declared with a function name, which helps for a more readable code while a function expression is declared without a function name.
🌐
DEV Community
dev.to › catherineisonline › function-declarations-function-expressions-2o5k
Function declaration vs expression - DEV Community
August 13, 2024 - We declare a function with a Function constructor and give it a name ... If you create a function with no name, it becomes a function expression.