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!
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
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
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?
Steven Parker225,742 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;