JavaScript JavaScript and the DOM Traversing the DOM Using previousElementSibling and insertBefore

Stephanie Franco
Stephanie Franco
8,566 Points

Declaring variable in the console is throwing an error

I'm trying to follow along by typing in the console, and I'm getting an error: VM91:1 Uncaught SyntaxError: Identifier 'li' has already been declared at <anonymous>:1:1

Steps to recreate:

  1. Launch workspace
  2. Click the eyeball to view in browser
  3. Open dev tools in browser > click Console
  4. Type let li = listUl.querySelectorAll('li')[3] and hit enter
  5. I get this error: VM91:1 Uncaught SyntaxError: Identifier 'li' has already been declared at <anonymous>:1:1
  6. if I just type li and hit enter, I get VM98:1 Uncaught ReferenceError: li is not defined at <anonymous>:1:1

Any idea what's causing this?

Steven Parker
Steven Parker
171,091 Points

To facilitate recreation of the issue, make a snapshot of your workspace and post the link to it here.

1 Answer

Steven Parker
Steven Parker
171,091 Points

I tried to replicate this issue using Chrome, but I didn't get the "already defined" error. I did, however, get the "li is not defined" error.

I'd guess this is because the console commands are executed in a temporary context, so anything created with "let" is immediately disposed. I didn't have the same issue using "var". But normally I would take advantage of implicit global declarations and not use either when establishing variables in the console.