Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStephanie Franco
9,167 PointsDeclaring 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:
- Launch workspace
- Click the eyeball to view in browser
- Open dev tools in browser > click Console
- Type let li = listUl.querySelectorAll('li')[3] and hit enter
- I get this error: VM91:1 Uncaught SyntaxError: Identifier 'li' has already been declared at <anonymous>:1:1
- 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?
1 Answer
Steven Parker
231,248 PointsI 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.
Steven Parker
231,248 PointsSteven Parker
231,248 PointsTo facilitate recreation of the issue, make a snapshot of your workspace and post the link to it here.