This question already has answers here:
Closed 4 years ago.
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
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.