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 Express Basics Deeper into Routing with Express Challenges: Redirects

Loris Yu
Loris Yu
6,294 Points

Why we need to "const name = req.cookies.username" twice in different "get" requests?

Why we need to "const name = req.cookies.username" twice in different "get" requests?

2 Answers

Sheila Anguiano
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Sheila Anguiano
Full Stack JavaScript Techdegree Graduate 35,239 Points

We need it, because two things:

  1. The scope of the const is LOCAL therefore the variable name only exist within each function
  2. A constant is defined as something that won't change so if you create the variable in the GLOBAL scope it cannot be changed from having a string (input name) and not having one (delete your name from the app). Also it makes it more readable and modular, thus easy to debug

Hope this helps