What’s the meaning of this “Function definitions may not appear within if statements, while loops, or any other statements.” I’m quite confuse with this statement.
What’s the meaning of this Function definitions may not appear within if statements, while
Share
One issue with what you’re reading in the book is that a function definition (which is different than a function assignment) is essentially hoisted to the top of the host function in some browsers so putting it inside a statement (like inside an
ifstatement) is downright misleading. The code will make it look like the function will only be defined if that branch of theifstatement executes, but that will not necessarily be the case. So, it’s a bad practice. It probably works in many cases, but is a bad practice.So, rather than this:
Put the internal function up at the top;
FYI, in strict mode internal function definitions are only allowed at the top. Condition function assignments can always be done with this syntax: