I think that everyone who starts programming in JavaScript will sooner or later ask themselves a question. I would reformulate your question:
Should I use (prefer to use) a function declaration (function statement) or function expression (var version)?
In most cases, you can write good JavaScript code using only one of the constructs. It is clear that there are some important differences in semantics, but I want to emphasize that, in my opinion, the answer to the question is mainly the answer about the style of the program . Therefore, I would answer that in the most real cases the choice of the question of taste .
People who prefer to use a function operator use it mostly not when they need to define a readonly function variable, and not why they do not want to declare it before using it. In my opinion, he uses it mainly because he likes the shape.
So, I think that there is no objectively correct answer to your question. The choice is subjective. Therefore, I write in my answer which design I personally prefer and in which situations.
My first languages ββwere Pascal, C, Fortran, C ++, etc. I used C # now. So when I started writing JavaScript programs, at first I used my existing style of writing programs from other languages. Later, I changed the style of my JavaScript code to match the specifics of the language.
I personally prefer to use a function expression style, and I declare all functions in the first expression of an external function. I find that the form, which is mostly understood by JavaScript semantics, where the function name is a variable, contains the value of the function. The function name obeys hoisting , like any other variable. For example, my code looks below
(function() { "use strict"; var myFunc1 = function (x) {
I use the function operator very rarely, and only if I declare the class constructor as follows:
(function() { "use strict"; function MyClass(x) {
I try never to use the third form:
(function() { "use strict"; var myFunc1 = function myFunc2(x) { ... }; ... }());
where myFunc2 declared in addition to myFunc1 . The implementation of this form depends on the web browser. It makes sense, probably in the case of using recursive functions.