1

how is it even possible to declare a function inside an IfStatement ? because according to the ecmascript spec only a Statement can be there.

if ( Expression ) Statement

if ( Expression ) Statement else Statement

and FunctionDeclaration isn't a Statement but it's a Declaration

so maybe i've missed something or maybe Browsers cannot implement such thing nowadays because it may ruin websites that use function declarations inside an if statement.

AngryJohn
  • 576
  • 4
  • 10
  • 1
    The way specs are written in general is confusing. It is understandable where your confusion comes from. Please take a look at the [list of **Statements**](https://tc39.es/ecma262/multipage/grammar-summary.html#sec-statements) in the spec's Grammar Summary. There you will find all things considered to be _statements_ categorized so they are easier to think about. Under the Statement category called Hoistable Declarations - you will, in-fact, find that a FunctionDeclaration is in the list of things that are statement. Hope this helps. – Randy Casburn Nov 09 '21 at 01:50
  • @RandyCasburn A *FunctionDeclaration* may be listed in the statements section of the spec, but that doesn't mean there is a production that makes it a *Statement*. – Bergi Nov 09 '21 at 02:14
  • @Bergi - you could say exactly the same thing about an expression (_doesn't mean there is a production that makes an Expression a Statement_) – Randy Casburn Nov 09 '21 at 02:18
  • @RandyCasburn Well there's the [*ExpressionStatement* production](https://tc39.es/ecma262/#prod-ExpressionStatement) which does just that (sans the semicolon). There is no such production for function declarations. A *FunctionDeclaration* is a not a *Statement*. – Bergi Nov 09 '21 at 02:19
  • @RandyCasburn but Expression is a Statement because a Statement Can be an ExpressionStatement and ExpressionStatement can be an Expression. – AngryJohn Nov 09 '21 at 02:20
  • @Bergi - gotcha. – Randy Casburn Nov 09 '21 at 02:23
  • @Bergi - you should get all that ambiguity removed! :-) – Randy Casburn Nov 09 '21 at 02:40

2 Answers2

3

It is not possible to put a function declaration in an if statement. Try

if (true) function example() {}

in strict mode - you'll get a SyntaxError. To declare a function inside a conditional branch, you must put it inside a Block, which is a Statement (through BlockStatement) and can contain Declarations (in a StatementList).

Maybe Browsers cannot implement such thing nowadays because it may ruin websites that use function declarations inside an if statement.

Yes indeed. For web legacy compatibility, function declarations in if statements are allowed in sloppy mode. Don't write code like that, but browser are allowed to treat it as valid.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • [Don't write code like that](https://stackoverflow.com/questions/58619924/function-declaration-in-block-moving-temporary-value-outside-of-block/58620404#58620404) ... – Jonas Wilms Dec 01 '21 at 21:37
-2

From the spec:

At the top level of a function or script, inner function declarations are treated like var declarations.

The chain goes like this.

Statement => BlockStatement => StatementList => StatementListItem => Declaration => VarDeclaration

It's all right here, statement list item

https://tc39.es/ecma262/#prod-StatementListItem

Prior to ECMAScript 2015, the ECMAScript specification did not define the occurrence of a FunctionDeclaration as an element of a Block statement's StatementList. However, support for that form of FunctionDeclaration was an allowable extension and most browser-hosted ECMAScript implementations permitted them.

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
  • statements can contain var declarations – Steven Spungin Nov 09 '21 at 01:39
  • yes that's right VariableStatement. but what about functions ? – AngryJohn Nov 09 '21 at 01:41
  • statements contain statementlist and statementlist contains var declarations – Steven Spungin Nov 09 '21 at 01:42
  • dude but i haven't spoked about the Block statement. IfStatement doesn't have to contain the BlockStatement it can contain other type of statements and function declarations aren't one of them that's where my whole confusion comes. – AngryJohn Nov 09 '21 at 01:45
  • look at the link... – Steven Spungin Nov 09 '21 at 01:46
  • it's says that Statementlist can be a StatementListItem and StatementListItem can be a Statement or Declaration. Statement if they could be a Statementlist that's mean that you could write a whole Javascript code inside an ifStatement without even using curly braces – AngryJohn Nov 09 '21 at 01:51
  • it says an if block can be a statement, and statement can have a statement list, and a statement list can have a var declaration, and a function is treated as a var declaration. – Steven Spungin Nov 09 '21 at 01:55
  • the curly braces declare the statement list. thats why you need them if having more than one statement list item, and why you can omit them if only 1 item – Steven Spungin Nov 09 '21 at 01:56
  • @StevenSpungin "*statement can have a statement list*" - no. Where do you see such a production? – Bergi Nov 09 '21 at 02:16
  • statement can be a block statement, that can have a statement list. read my post fully @bergi. last paragraph. 'Prior to ECMAScript 2015, the ECMAScript specification did not define the occurrence of a FunctionDeclaration as an element of a Block statement's StatementList' – Steven Spungin Nov 09 '21 at 13:22
  • Ah, yes, but that is a *contains* relation, not an *is-a* relation. – Bergi Nov 09 '21 at 13:59