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.
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" .
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.
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.
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.
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!";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With