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 trial

JavaScript JavaScript and the DOM (Retiring) Getting a Handle on the DOM Select All Elements of a Particular Type

tal Shnitzer
PLUS
tal Shnitzer
Courses Plus Student 5,242 Points

the const "myParagraph" is not defined on my console

const myParagraph = document.getElementsByTagName('p')

<undefined

myParagraph.length

<VM741:2 Uncaught ReferenceError: myParagraph is not defined

myParagraph[0]

<VM742:2 Uncaught ReferenceError: myParagraph is not defined

3 Answers

Steven Parker
Steven Parker
231,270 Points

The first response is normal, the "undefined" you see there is indicating the fact that a "const" directive does not return a value.

As to the others, I tried this myself using Chrome, and got the same results. But if I use "var" instead of "const", it works. My guess is that the console commands are executed within a code block that causes anything declared with "const" or "let" to be immediately disposed. This might qualify as a bug in the console, but you may want to do a bit of research before reporting it.

This was helpful, thanks.