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 JavaScript Basics (Retired) Storing and Tracking Information with Variables Working with Strings and Finding Help

Joel Hernandez
seal-mask
.a{fill-rule:evenodd;}techdegree
Joel Hernandez
Front End Web Development Techdegree Student 2,571 Points

Proper syntax to use?

Okay so in this video, when you build the shout app, Dave writes the code like this

var stringToShout = prompt("What should I shout?");
var shout = stringToShout.toUpperCase();
shout += "!!!";
alert(shout);

Is there a reason he wrote the syntax like that? Wouldn't we want to use syntax like this?

var stringToShout = prompt("What should I shout?");
alert (stringToShout.toUpperCase() + "!!!!");

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Joel,

I think the reason Dave demonstrates it the way he does is because it is a "JavaScript Basics" course. This way he kind of shows the long, but detailed way the code is achieving the outcome.

Absolutely, the way you stated is much cleaner and DRY, and is the way I (and probably most experienced coders would do it), but to the 'basics learner,' it doesn't really show how you get to that point.

I've also come to learn that there are many ways to do the same thing in JavaScript, so Thank-You for sharing. It adds to and teaches other, often more efficient ways of coding.

:)

Joel Hernandez
seal-mask
.a{fill-rule:evenodd;}techdegree
Joel Hernandez
Front End Web Development Techdegree Student 2,571 Points

Thank you for the answer. This is my first time coding in JavaScript as well so I just want to make sure I get the proper syntax formatting down. Oddly enough, the first method is a little more confusing to me. However, I now understand why he does it this way.

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

I'm glad it makes sense. DRY code is always better for you and for other programmers looking at your code. However, in the challenges here on Treehouse, they can be very specific. So, if you know your code is correct, but it is not the way the challenge asked for it, often it will not let you pass.

Definitely don't stop experimenting and coming up with cleaner and better code, but just be wary of the challenges in the course.

You do seem to have and strong understanding of the JavaScript syntax. Good luck in your JS journey. :)