12

I am trying to write some ES6 code in chrome console but i ran with some errors. How can i run the ES6 scripts in console?

For example, given the input

let type='grizzle';

the console records a SyntaxError with the message

Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

as shown in the following screenshot

Screenshot of Chrome console response

Jeremy
  • 2,642
  • 18
  • 36
iamsuman
  • 1,413
  • 19
  • 32
  • This was marked as duplicate to a question about strict mode. However, strict mode (which is an ES5 feature) has nothing to do with ES6 so I'm reopening. – slebetman Sep 14 '15 at 03:12
  • Check out this answer: http://stackoverflow.com/a/24011617/4361297 – Saad Sep 14 '15 at 03:15
  • @slebetman the ES6 declarations `let` and `const` behave differently in strict mode, so it is actually relevant. – Jeremy Sep 14 '15 at 03:17
  • 1
    @OP in the future, please consider typing the code and exception details in question, rather than posting a screenshot – Jeremy Sep 14 '15 at 03:20
  • @saadq your suggested duplicate actually covers *enabling* ES6 features in Chrome, but in Chrome 45 the `let` declaration (which OP is using) is now enabled by default. OP's issue is different: the javascript block needs to be in strict mode. – Jeremy Sep 14 '15 at 03:30
  • 1
    @Jeremy If the OP's problem is really different, then show us by editing it to emphasize how it's different? From where I'm sitting it's a duplicate of the post I closed it as a duplicate of. – George Stocker Sep 15 '15 at 15:54
  • @GeorgeStocker there's some very strange confusing things going on here. OP's problem is *similar* to http://stackoverflow.com/questions/24369328/, which is "how do I use 'use strict' in JS console", and marking it as a duplicate of *that* might be fair. The question is **NOT** a duplicate of http://stackoverflow.com/questions/24008366/, which is "how do I get Chrome to support ES6". Chrome v45 already supports ES6, so the current "duplicate" marking is totally irrelevant. If I edit the question to emphasise that, I will make the question worse. – Jeremy Sep 15 '15 at 22:18
  • @GeorgeStocker nonetheless I will give it a go, and you or someone else can knock back the edit if you wish. – Jeremy Sep 15 '15 at 22:21

1 Answers1

12

UPDATE: As of late 2019, you can just use let in the console.


As the error message states, some ES6 features are not available outside of strict mode. Therefore, to take advantage of those features, you must first create a strict-mode block.

From the console, the easiest way to use strict mode is by creating an Immediately-Invoked Function Expression (IIFE). For example,

(function() { "use strict"; let x = "asdf"; }());

will behave as intended when entered in the console.

Jeremy
  • 2,642
  • 18
  • 36
  • 1
    You actually don't need 'use strict' for let. An arrow IIFE. `(() => {let a = "Hello world"; console.log(a)})()` – Ben Butterworth Dec 16 '19 at 08:50
  • @ZenBoat thanks for pointing that out — seems like whatever restriction they had back in the day, they've now removed. – Jeremy Dec 16 '19 at 22:27