Snippets - Identifier Already Announced

I wrote this in a Chrome snippet:

let myVar = someValue; 

And when I try to run it a second time, it says that the variable has already been declared and throws an error in the first line.

Error:

Uncaught SyntaxError: Identifier 'myVar' already declared at: 1: 1

And, of course, this will be the default behavior for the console, but it doesn't seem to make much sense here ..

Is this intended? Is there any way around this?

+11
source share
8 answers

I believe that you are faced with the fact that the let statement can only be used to create a variable once in any given area. In your example, even if you use Chrome fragments, if you window.commitPromotionData right after the let statement, you will see that it is. That the scope of your let statement assigns to a variable. Restarting the same fragment essentially attempts to create the same variable in the window and results in a syntax error, as described here .

You have two ways:

  • Obviously, the first is to convert the top-level let statements to var
  • Or create a new block area to package the code. This can be done, for example, by transferring the code to IIFE (function(){ ... code ... })()
+11
source

Use the scope of the block and wrap it in '{}'.

 { enter code here } 
+6
source

The error is that you specified the variable in the scope twice.

You can reload or refresh the page, then you will get a new area, the error will disappear.

+2
source

Move to es6

 ;(() => { console.log('local scope') }() 
0
source

Create a local scope for your code snippets. Then after completion, let life cycle be completed. You can run your program again and again.

Rush!!!

 { let a = 10; console.log(a); } 

  { let commitPromotionData = (product) => new Promise((resolve, reject) => { //Do something with the server var statusCode = 200; statusCode == 200 ? resolve(product) : reject(); }); } 
0
source

This is because somewhere in another fragment you used a variable with the same name. Make sure you save your snippet, and then refresh the page.

To prevent this from happening, use {} around your code snippet to create a block so that the variable inside it is inaccessible from the outside.

0
source

just pause the script. which is located in the upper right corner of the console

0
source

LONG SHORT SHOT :. WHEN THAT THE POP UP ERROR just press the SOURCE button OPTIONS NEAR THE CONSOLE AND THE PAUSE OF EXCEPTIONS, which is the seventh OPTIONS LEFT, now you work well :) IN THE CONSOLE AND PERFORM YOUR WORK PROGRAM FINE ... EXCELLENT ..

-2
source

Source: https://habr.com/ru/post/1014154/


All Articles