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

Why do I need to create separate variables?

I was confused by the creation of what seem to be unnecessary variables in the "RSVP Checkbox" and "Removing Names" lessons. I'm assuming what I'm seeing is "best practice," but I don't understand why.

For example, in the first lesson, the code looked something like this:

const checkbox = event.target; const checked = checkbox.checked; if (checked) . . .

Why is this better practice than simply writing: if (event.target.checked) . . .

In the next lesson, there was a similar case.

const li = e.target.parentNode; ul.removeChild(li);

Why is this used rather than: ul.removeChild(e.target.parentNode);

2 Answers

Hey Angela. It can seem tedious to assign everything to a unique variable, and it isn't always 100% necessary, but there are some good reasons for doing it at least most of the time.

Assigning each meaningful component of a script you are writing it's own unique variable is a good practice because:

  1. It makes your code more readable. If you come back to it a few weeks or months later, you will be able to understand what you were doing more quickly if each element and piece of data has its own variable with a descriptive name.

  2. It looks better to your colleagues and prospective employers. In a professional setting other developers will have to read your code, and even though you know what the code means, they may not and assigning good descriptive variable names helps others understand your code. This is also especially true if you are applying for jobs and want to show off your projects to prospective employers. An employer will be much more likely to consider you if the code you write is clean and easy to follow.

  3. It makes your code more DRY. You may have come across the DRY principle - Don't Repeat Yourself. If you're writing clientside vanilla JavaScript, you may end up using event.target a lot. Instead of writing it many times inside an event listener, its better to write it once and assign it to a variable and just reuse that variable. Variables make your code more DRY and modular.

If you're writing a very small script, then assigning everything a variable might not be necessary. But as soon as you start writing anything bigger than a small little script, it's good to assign variables--especially if you plan to modify the code in the future or show it off to anyone. I hope this answer helps!

Thank you, Michael, for your excellent response!