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 DOM Scripting By Example Adding and Removing Names Registering Names

Justin Hicks
Justin Hicks
14,290 Points

I have question on if there is any unforeseen issue with how I did Registering Names

The way I did it differs from how it was done in the video. Is there an issue with doing it this way I don't know about?

const form = document.getElementById("registrar");
const text = form.querySelector('input');

form.addEventListener('submit', (event) => {
  event.preventDefault();
  let ul = document.getElementById('invitedList');
  let li = document.createElement('li');
  li.innerHTML = text.value;
  text.value = '';
  ul.appendChild(li);
});

1 Answer

By using let instead of const you leave your code open to assigning a new object to the variables (ui and li) which may happen unintentionally at some time in the future by you or someone else maintaining your code. Once ui and li are assigned to the DOM objects, you don't intend them to be changed to some other objects, so you should use const which would cause an exception (alerting you of the problem and allowing it to be fixed before going to production) if you attempt to reassign or delete the variable. The rule of thumb is use const first and let only if you need to reassign a variable at some point (like in a for loop).