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 trialywang04
6,762 PointsWhich elements can be defined as const variable?
Noticed that like input, button, div elements are defined as const variables in the video while ul, li elements are defined as let variables.
//div, button, input elements are defined as const variables
const toggleList = document.querySelector('#toggleList');
const listDiv = document.querySelector('.list');
const descriptionInput = document.querySelector('input.description');
//ul, li elements are defined as let variables
let ul = document.getElementsByTagName('ul');
let li = document.createElement('li');
Is there any convention for defining different elements in JavaScript? I know what the difference between const and let. I just want to know how to use them for different elements. Thanks a lot.
1 Answer
Lars Reimann
11,816 PointsI usually follow the convention, that every variable is declared as const, except when I want to reassign to it or don't know the value of the variable when I declared it. In this case I use let.
What might have been done here is that variables containing results from some getElementBy... operation are contained in a let variable, because the node lists you get back from these methods is dynamic. This means that they change when you add more matching elements to the DOM, so they are not constant in this sense. Opposed to this, node lists returned from querySelector and querySelectorAll are static; they don't change later on.
ywang04
6,762 Pointsywang04
6,762 PointsThanks a lot. It's clear to me. :)