Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can comments come before `use strict;`?

I've seen a few places around the internet passively stating 'use strict;' must come on the first line of the functional scope for which you want the behavioral directive to apply.

However, in my experience, it doesn't matter if there are comments before it.

/* some comment */ 'use strict'; 

Is there a functional deficiency with having comments come before the directive, or is it purely a matter of style? Is this defined anywhere in the ECMAScript specification?

I'm asking not only for V8 (node.js) environments, but for browsers as well.

like image 385
Qix - MONICA WAS MISTREATED Avatar asked Jul 14 '15 17:07

Qix - MONICA WAS MISTREATED


People also ask

Does use strict have to be at the top?

Please make sure that "use strict" is at the top of your scripts, otherwise strict mode may not be enabled. Only comments may appear above "use strict" .

What is not allowed in strict mode?

Not Allowed in Strict Mode Objects are variables too. Deleting a variable (or object) is not allowed. Deleting a function is not allowed. For security reasons, eval() is not allowed to create variables in the scope from which it was called.

When would you not use strict mode?

If you have such an unrestrictedly typed code, that is used variables without declaring. One variable declared within some function/scope and used from somewhere else(it will be undeclared there) and you can't rewrite/change them, then you should not go for "use strict;" mode because it will break the code.

Where should we place use strict?

We can also apply this to functions. To do this, we add the statement "use strict" or 'use strict' inside the functions on the top of the body, before any other statements. It's applied to everything inside, including functions that are nested in the function that uses strict mode.


1 Answers

Yes you can add comments before "use strict";--it just must appear before any statements.

See this example from MDN

// Whole-script strict mode syntax "use strict"; var v = "Hi!  I'm a strict mode script!"; 
like image 53
Dave Avatar answered Sep 17 '22 20:09

Dave