26

With this page:

<!DOCTYPE html>
<html>
  <head>
    <script>
        "use strict";
        var foo = 2;
        delete foo;
    </script>
  </head>
  <body></body>
</html>

Firebug console gives:

applying the 'delete' operator to an unqualified name is deprecated
>>> foo
ReferenceError: foo is not defined
foo

But then this is successful:

>>> var bar = 2;
undefined
>>> delete bar;
true

Even if you comment out delete foo; so that the script does not break, deleting bar is still successful despite the fact it "is a property of a Global object as it is created via variable declaration and so has DontDelete attribute":

>>> foo
2
>>> delete foo
false
>>> var bar = 2;
undefined
>>> delete bar
true

Is it possible to enable "strict mode" in FireBug and or Chrome's console?

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
AJP
  • 26,547
  • 23
  • 88
  • 127
  • 1
    I wonder if the console code is being piped through `eval()`, in which case the DontDelete attributes won't be set. – AJP Jul 26 '12 at 20:34
  • ...right, if I had just read then next section: `And this is the gist of Firebug’s abnormal behavior. All the text in console seems to be parsed and executed as Eval code, not as a Global or Function one.` – AJP Jul 26 '12 at 20:34
  • 2
    I followed @zoranc's first suggestion just so I could see strict mode working in chrome's console. `(function f() { 'use strict'; console.log('"this" here is: ', this, 'Strict Mode is cool...'); } ());` `(function f() { console.log('"this" here is: ', this, 'Global variables are evil! So Crockford told me...'); } ());` – slacktracer Jul 28 '12 at 00:52
  • devtools console should really have a checkbox to enable strict mode for all commands – Rivenfall Jul 27 '20 at 08:38

5 Answers5

16

The firebug console works by wrapping all the code in an "eval" call so the first statement in your script is no longer "use strict" - hence it is disabled. You could try wrapping your code in a function to enforce "use strict" for that particular function but the best solution I know of is to skip the console and test straight in the page itself.

zoranc
  • 2,410
  • 1
  • 21
  • 34
5

use shift+enter to input 'use strict'

like this enter image description here

Sola Zhou
  • 169
  • 2
  • 3
2

If you are just testing a single function in the console, you can also just put 'use strict' as the first line in the function declaration.

P Ackerman
  • 2,266
  • 20
  • 24
2

Chrome: put 'use strict'; prefix in your code line (and/or shift+enter for multiline)

'use strict'; var foo = 2; delete foo;
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
-2

On Chrome go to "chrome://flags", then "Enable Experimental JavaScript". Relaunch.

solf
  • 1