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 Editing and Filtering Names Saving Names

Why are we re-declaring the input and the span variables in the second if else statement?

I'm not sure I understand why we are redeclaring the input and the span variables again. Since we used CONST to declare them in the first ELSE IF block, how come we can change their values in the second ELSE IF block.

I hope the question is understandable :D

Dejan

3 Answers

Steven Parker
Steven Parker
229,670 Points

A const has block scope.

This means that the one declared in the first block cannot be accessed from the second block. So while the second block may be re-using the name, the variable itself is completely different from the one in the first block.

Thanks Steven! :)

So, if I understand correctly, const and let both have block scope, while var doesn't have block scope?

Steven Parker
Steven Parker
229,670 Points

You got it. :+1: A var has function scope instead.

Roudy Antenor
Roudy Antenor
4,124 Points

Big Thanks Steven - Hey Dejan i had the same question- many Thanks for asking it first!