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 Introducing the Practice

Where to use methods, objects, functions to convert variables

Hi! I'm looking for clarification on when to convert a variable. I've seen it done 2 different ways:

  1. When declaring the variable. Ex: name = prompt("What's your name?").toUpperCase();

  2. In a conditional statement. Ex: if (name.toUppercase() === 'Jane') ....

Does it matter where it's converted?

1 Answer

Steven Parker
Steven Parker
231,269 Points

If the converted value is only used one time, where it is done is most likely just a matter of programmer preference.

But if it is used multiple times, it is more efficient to convert and store it just once, before the first time it is used.