9
"use strict";

if (true) {
  function foo() {
  }
}

In PhpStorm this code shows an error:

Function statement not at top level of a program or function is prohibited

However, Chrome happily executes it, even in the debugger and without any console output.

Now is it prohibited or not?

AndreKR
  • 30,401
  • 15
  • 95
  • 155
  • 2
    It shouldn't be allowed. – Oriol Aug 06 '15 at 16:41
  • Possible duplicate of [Why Chrome still keep silent when using functions inside blocks in “strict mode”?](http://stackoverflow.com/questions/36756010/why-chrome-still-keep-silent-when-using-functions-inside-blocks-in-strict-mode) – Jeremy Apr 21 '16 at 04:17

1 Answers1

9

Yes, in ES5 they are prohibited (and in strict mode, all implementations throw). See also Kangax' great article for function statements in sloppy mode.

However, in ES6 they are block-level function declarations with new semantics. See also What are the precise semantics of block-level functions in ES6?. This seems to be what Chrome implements here; foo is not available outside of the if block.

Community
  • 1
  • 1
Bergi
  • 572,313
  • 128
  • 898
  • 1,281
  • So my Chrome implements the latest spec while my PhpStorm is behind? My JS language level is set to `JSX Harmony`. – AndreKR Aug 06 '15 at 16:54
  • @AndreKR: That's likely. I'm not familiar with PhpStorm or "JSX Harmony", but given that ES Harmony is an ongoing process this seems to be dubious :-) Are you using ES6 yet? – Bergi Aug 06 '15 at 16:57
  • I'm not using it but while investigating this I changed the language level. The error shows up with language level set to "ECMAScript 6", too. – AndreKR Aug 06 '15 at 17:06