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 Redirects

Name Variable

In this video when Andrew assigns the name variable to the cookie:

"const name = req.cookies.username";

"res.render('index', { name });"

I am guessing this extra code is completely optional?? and that:

"res.render('index', { name: req.cookies.username });"

Is still acceptable?? for me the original method is much easier to understand...

1 Answer

David Ray
David Ray
6,691 Points

You're right they're interchangeable, meaning you can either create a variable and assign it to req.cookies.username or just put req.cookies.username straight into the render. However by creating a variable and assigning it first, this allows for more flexibility with your code (i.e. you can redefine the name variable to something else if needs be, or reuse the variable without having to type req.cookies.username over and over again) but also this allows better readability for other developers or yourself at a later stage.