0

Here is a simple example: I have two functions: someFunction and someFunction2.

someFunction recognizes someFunction2 event thoug it is defined after someFunction. It is different with the object someVar thoug. If it is defined after the function that uses it, it is not recognized by the function hence undefined. But if the object is defined before the funcution that uses it the function itself recognizes the objec. Here is an example that showcase:

function someFunction() {
    console.log(someVar)
    someFunction2()
}

someFunction()

function someFunction2() {
    console.log("Some Function 2 has been called")
}

var someVar = 'Some Var Displayed Here'

In the case above someFunction recognizes someFunction2 but doesn't recognize the object someVar WHY? what is the difference between the someFunction2 and someVar?

Hairi
  • 3,318
  • 2
  • 29
  • 68
  • Obviously because `someFunction()` is called **before** `someVar` definition. Just move `someFunction()` call somewhere after `someVar`. – hindmost Dec 25 '17 at 14:50
  • @hindmost I know that but this is not the case with `someFunct2`. What is the difference in calling `someFunction2` or evoking `someVar`? – Hairi Dec 25 '17 at 15:02
  • 3
    Named function declarations and variable declarations are hoisted, but assignment statements are not. – 4castle Dec 25 '17 at 15:06
  • @4castle If you suggest this as an answer I would accept it! – Hairi Dec 25 '17 at 17:32
  • Then is a function definition a function declaration as well? – Hairi Dec 25 '17 at 17:33
  • Once a question is marked as a duplicate, it can't get new answers. – 4castle Dec 25 '17 at 17:34
  • It is very sad. If I knew the about the term `hoisting` I wouldn't ask such a question at all. – Hairi Dec 25 '17 at 17:35
  • A "function definition" isn't a phrase the JavaScript community uses, so I'm not sure what it would be. – 4castle Dec 25 '17 at 17:36
  • Anyway, function is acknowledged event if it is "defined" after its invocation, but with variables like string var for e.g. is not. That is very strange to me. – Hairi Dec 25 '17 at 17:41

0 Answers0