Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use ES6 syntax such as let in chrome console? [duplicate]

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

like image 964
iamsuman Avatar asked Sep 14 '15 01:09

iamsuman


People also ask

How do I enable ES6 in Chrome?

5 Answers. Show activity on this post. In Chrome, most of the ES6 features are hidden behind a flag called "Experimental JavaScript features". Visit chrome://flags/#enable-javascript-harmony , enable this flag, restart Chrome and you will get many new features.

Does Google Chrome support ES6?

Safari, Chrome, Firefox and Edge all support the ES6 Modules import syntax.

How do I enable the JavaScript console in Chrome?

Press Command+Option+J (Mac) or Control+Shift+J (Windows, Linux, ChromeOS) to open the Console, right here on this very page.


1 Answers

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.

like image 182
Jeremy Avatar answered Oct 23 '22 09:10

Jeremy