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

B G
B G
1,866 Points

Breakdown of stringToShout

Can someone give a breakdown of the stringTOShout code? I would like to know everything it is doing. This would help me better understand JS a bit more. This is what I have:

var stringToShout = prompt("What should I shout?"); (The variable is stringToShout and the variable = prompt to make a pop up asking the question "What should I shout?"

var shout = "stringToShout.toUpperCase(); (another variable is made "shout" and the variable = "stringToShout.toupperCase adding a property to it that makes what is typed in capitalized )

var shout += "!!!"; (this is using short hand concadence to select the variable shout which already contains the string "stringToShout and adding !!! to the message. I am confused about the +=. is this shorthand for shout + "stringToShout" + "!!!";? Does this take the typed information and apply capitalization and "!!!" to the users information?)

alert(shout); (This makes a pop up alert showing what the user typed in with capitalization and "!!!". The first variable stores the information the user types in the prompt. The second and third variables take that stored information make it uppercase and adds !!! to what the user types in. The alert pops up with the stored information and the new string information)

I wanted to make sure I am understanding what the code is actually doing. I am a little unsure about the entire process. How is the information typed in transferred into the alert? Is what the user types in considered a new string?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Let's start by taking another look at the code:

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

So let's start with the first line as it seems there might be some confusion on what is happening here. When we're doing an assignment it's the value on the right side that's being assigned to the variable on the left. That value can be the result of an expression or something returned by another function which is the case here. The confusion here might be that you believe that what's on the right side is the prompt. That's not exactly the case. When we run the prompt it asks the user to type in something. Whatever is typed in will come back(be returned) as a string. This string is then assigned to stringToShout.

For example, if the user had typed "Hello", then the string returned would be "Hello". If you were to then console.log(stringToShout), it would log out "Hello".

The second line takes the string stored in stringToShout and turns it into upper case. Remember, we said that the thing on the right (or the result of the thing on the right) gets assigned to the variable on the left. The result of running toUpperCase() on stringToShout would be "HELLO" per our previous example. At this point, console.log(shout) would log out "HELLO". At this point in time, the value of stringToShout is still "Hello", but the value stored in shout is "HELLO".

You are correct about the third line. It is short hand for concatenation. You could have instead written the same line like this with the same results:

shout = shout + "!!!";

As stated earlier, shout would now contain "HELLO" and the three exlcamation points would be concatenated onto the end resulting in "HELLO!!!".

The fourth and final line simply puts the value of shout which is now "HELLO!!!" into an alert back to the user.

Hope this helps! :sparkles:

B G
B G
1,866 Points

Great stuff. Thank you very much! I am still a little hazy on the use of += It will hopefully click