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

Tyler McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,700 Points

Why do I have to use `name = name.trim();` instead of just `name.trim();`?

Why do I have to use name = name.trim(); instead of just name.trim();?

When I first ran my code I was not redeclaring the name variable each time, and it was not working. I realized this was incorrect. Why?

1 Answer

Steven Parker
Steven Parker
231,007 Points

Just like calling an independent function, calling a method causes code to run, but doesn't store the result anywhere. By adding the assignment, you capture the result and save it back in the variable.

It would be similar to putting x + 5; on a line, as compared to x = x + 5;