60

I am practicing JavaScript on Chrome's 'JavaScript Console' ( version: 35.0) and I am unable to use the 'use strict' clause as expected.

For the following code snippet :

var obj={x:1,y:2}

//Define new property with 'writable' flag as false.
Object.defineProperty(obj, "z", {value:3, writable:false, enumerable:false, configurable:false})

// Try to change the property 'z',
"use strict"; obj["z"]=4

Output: 4

As per my understanding, changing value of a 'non-writable' property will silently fail in non-strict mode and throw 'TypeError' in strict mode, But I don't see the exception.

console.log(obj)

Object {x: 1, y: 2, z: 3}

Even though the property value is not changed but I am expecting a exception. Please correct if I am doing something wrong ?

danwellman
  • 9,068
  • 8
  • 60
  • 88
Pradeep
  • 1,057
  • 2
  • 9
  • 17
  • see http://stackoverflow.com/questions/11677452/possible-to-enable-strict-mode-in-firebug-and-chromes-console – SNAG Jun 23 '14 at 15:13
  • `(function() {"use strict"; obj["z"]=4})();` ~ *TypeError: Cannot assign to read only property 'z' of #* – Alex K. Jun 23 '14 at 15:14
  • @SNAG: That has a clear answer for Firebug, but not Chrome. (Despite the title.) Which I guess makes this a duplicate of that question, except that question really only focusses (in both the question and answer) on Firebug, and really should be separate questions. – T.J. Crowder Jun 23 '14 at 15:15
  • @SNAG That answer does not answer this question. This question is not a duplicate of that question. – DavidS Dec 02 '15 at 00:49
  • `(x=>{'use strict'; 01})()` – Kamil Kiełczewski May 02 '19 at 12:25

1 Answers1

86

The easiest way to use strict mode is to use an IIFE (immediately Invoked Function Expression) like so:

(function()
{
    'use strict';
    var foo = 123;//works fine
    bar = 345;//ReferenceError: bar is not defined
}());

To create a new-line in the console, use shift + enter, or write your code in a separate editor first, then copy-paste it to the console. Setting up a fiddle is all fine and dandy, but just test your code with the markup it was written for (ie: just clear the browser cache and test).
However, I'd urge you to install node.js, still. It's a lot easier to test your code, or validate it (both syntactically and coding-style wise) using JSHint. There are also a lot of ways to examine and your code that run out of node.js, so it's a really good development tool to have

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • If only there was a way to inject this into chrome developer tools as well as a "use strict" checkbox. – user420667 Mar 15 '16 at 23:40
  • @user420667: That would be really tricky, because the dev tools console basically relies on `eval` for a lot of things, and `eval` + strict mode don't play nice – Elias Van Ootegem Mar 16 '16 at 09:51
  • 1
    Although it may be obvious to some, it's worth noting that *only* the block of code within the IIFE will be run in strict mode. This code won't affect subsequent commands in the console. – shennan Apr 26 '18 at 17:08
  • 3
    Actually you can just write `'use strict'` before your code without the need to wrap it within an IIFE. – Qwerty May 16 '18 at 18:12
  • @qwerty this post is 4 years old, and specifically relates to strict mode in chrome' s console, not in a file. Maybe the console has changed, but I'm no longer writing client side anyway – Elias Van Ootegem May 16 '18 at 18:21
  • Of course, I meant in a console. And although this answer is 4 years old, it still returns from google search. – Qwerty May 17 '18 at 07:31
  • @Qwerty It does not work (anymore?). Try this on console, it should print undefined but it doesn't and instead returns the global window object: `'use strict'; console.log(this);` – Shayan Aug 07 '22 at 06:56
  • 2
    @Shayan Honestly I am not sure whether this example in particular should return `undefined`. Try this `'use strict'; (function() { console.log(this)} )()`. Works as expected - strict mode is applied. – Qwerty Aug 09 '22 at 18:53