user3367792
user3367792

Reputation:

Do javascript functions need to be declared in the reverse order?

Elaboration: If I would have, for example the following code:

//Javascript example
var alice = function() {
    var value = bob() + 1;
    console.log value
}
var bob = function() {
    var value = 1;
    return value;
}

//Running function A
alice()

Would I have to declare function B first, because I am calling it in function A without reaching function B yet.

Upvotes: 2

Views: 45

Answers (3)

invernomuto
invernomuto

Reputation: 10211

The only limitation is the definition of every function before utilization

Upvotes: 0

Quentin
Quentin

Reputation: 943981

No.

If you have a function declaration, then it will be hoisted and order doesn't matter (although putting functions in an order so that a function call doesn't appear before the function being called is good practice).

If you have function expressions (as you do in your example), then you need to have the functions created before they are called, noting that in this example none of them are called before the line alice() so only that line needs to be after the functions and the order of the functions themselves doesn't matter. (But the best practise principles described above hold).

Upvotes: 1

La-comadreja
La-comadreja

Reputation: 5755

No, because the Javascript interpreter will recognize the functions declared after and link them appropriately to the line in question.

Upvotes: 0

Related Questions