5

According to this article

Adding “use strict” as the first statement¹ in your JavaScript code will enforce Strict Mode over the entire

So why :

"use strict";
012; 

Doesn't throw errors

while

(function () {
    "use strict";
    012; })();

does ? (Octal literals are not allowed in strict mode.)

John resig says nothing about it. he just says :

Simple. Toss this at the top of a program to enable it for the whole script:

"use strict"; Or place it within a function to turn on strict mode only within that context.

function imStrict(){ "use strict"; // ... your code ... }

edit :

enter image description here

edit #2.

I tested the code in console.(chrome). in jsbin sample - it is working. still , I dont understand why it behave different in console.

7
  • What happens if you do "use strict"; 012; in one line?
    – melpomene
    Commented Jan 20, 2013 at 14:58
  • @melpomene same. just tested that.
    – Royi Namir
    Commented Jan 20, 2013 at 14:58
  • 1
    Well, then whatever you're using as a REPL doesn't support top-level "use strict".
    – melpomene
    Commented Jan 20, 2013 at 14:59
  • 2
    Don't test code in a console and expect identical behaviors.
    – the system
    Commented Jan 20, 2013 at 15:05
  • 1
    A console is not a pure environment. Use any other strict mode example, and you'll see that it disables a global strict declaration, for example foo="bar" (no declaration). This is not the case for application code, where it actually counts. Additionally, there are usually methods define in the console that are not available in application code. Bottom line... if you see odd behavior in the console, first verify in an actual application.
    – the system
    Commented Jan 20, 2013 at 15:24

3 Answers 3

2

It does throw an error.

quentin@workstation:~ # cat > tmp/foo.js
"use strict";
012; 

quentin@workstation:~ # node tmp/foo.js

/users/quentin/tmp/foo.js:2
012; 
^^^

module.js:434
  var compiledWrapper = runInThisContext(wrapper, filename, true);
                        ^
SyntaxError: Octal literals are not allowed in strict mode.
    at Module._compile (module.js:434:25)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Array.0 (module.js:484:10)
    at EventEmitter._tickCallback (node.js:190:38)
1
  • @RoyiNamir - So either there is something weird about the place you are executing it, or there is a bug there. The problem is your tool.
    – Quentin
    Commented Jan 20, 2013 at 14:58
1

The console doesn't behave the same way as other places, try opening the following in your browser and you'll see the error re-appear without the need to wrap it in a function.

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>use strict</title>
        <script>
"use strict";
012;
        </script>
    </head>
    <body>
    </body>
</html>

You can tell that the console is implemented differently to direct execution by typing just ~, +, etc (you'll get a SyntaxError: Unexpected token }).
It is possible to reproduce similar behaviour by writing code like this directly (I won't call it the same because I don't know how the console is doing it)

example: { // labeled block
    "use strict"; // your code
    012;
} // end of block, no SyntaxError thrown for strict
6
  • I assumed it should provide the same result.
    – Royi Namir
    Commented Jan 20, 2013 at 15:12
  • see my edit #2. Ill be glad to get a sample of which console doesnt behave like normal flowing code. ( except this sample).
    – Royi Namir
    Commented Jan 20, 2013 at 15:18
  • 2
    The difference is to do with how console is implemented; for example, try typing just ~, +, etc into the console and you'll get a SyntaxError: Unexpected token }, this means the code is being parsed and wrapped by the console before it is executed, which is different to executing it directly.
    – Paul S.
    Commented Jan 20, 2013 at 15:24
  • @RoyiNamir I hope my edit sheds some more light on it for you.
    – Paul S.
    Commented Jan 20, 2013 at 16:14
  • thanks. however the result in the console is 10. ( your last code). I guess console just handles that differently. ( everybody said it. nobody showed why).
    – Royi Namir
    Commented Jan 20, 2013 at 16:19
0

012 is an octal literal which is not allowed in strict mode as it is deprecated by edition 3 of ECMA-262. JavaScript 1.5 still supports that octal integer literal for backwards compatibility. and your example IS throwing an error!

you have 3 possibilities.

  1. remove that octal literal

  2. dont use the strict mode

  3. or you can use the octal literal first, and wrap all your code in an anonymous function and restrict the strict mode to your immediately invoked Function Expression (iife):

012;

(function () {
  "use strict";

  ... your code
})();
2
  • 2
    The question was "How to throw a strict-code error (with octal literal as demonstration) with program-level "use strict" (demo in console)?" Your answer is not related to that
    – Bergi
    Commented Jan 20, 2013 at 15:46
  • 1
    no, that was not the question. the question was, why the given example is not throwing an error (but it is!). Commented Jan 20, 2013 at 15:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.