Why Reassigning Const Producing Error In Console Only If We Do That In Code Editor?
Solution 1:
Assuming you're using Chrome, this is by design. const redeclarations was intentionally implemented in Chrome Devtools 92 as a means to make it easier for developers to test code within the Chrome console:
The Console now supports redeclaration of const statement, in addition to the existing let and class redeclarations. The inability to redeclare was a common annoyance for web developers who use the Console to experiment with new JavaScript code.
As the blog post points out, it enables you to redeclare const and other bindings you wouldn't usually be able to redeclare. This means something like:
> const a = 1;
> const a = 2;
is allowed as the two lines of code are executing in separate REPL scripts (indicated by the >), whereas performing:
> const a = 1;
const a = 2;
x UncaughtSyntaxError: Identifier'a' has already been declared
is not allowed as this is redeclaring const within the one REPL script (multiple lines of code can be entered into the one command line using SHIFT + ENTER).
This change is only for the Chrome devtools console, and not for regular page scripts, which will throw a SyntaxError as expected if you try and redeclare const.
Post a Comment for "Why Reassigning Const Producing Error In Console Only If We Do That In Code Editor?"