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 Working with Strings Display the Value of a String on a Page

Jasper Kop
Jasper Kop
10,423 Points

Why is there an extra line for the .toUpperCase() ?

it felt a bit to much so when I tried to rewrite the lines I came up with the next solution

const shout = prompt("What do you want to shout?"); //const shout= stringToShout.toUpperCase(); const shoutMessage=The message to shout is :${shout.toUpperCase()}!!;

console.log(shoutMessage)

I changed the variable of the first line. and it works but... there is probably a good reason why it has been written on a separate line.

3 Answers

Steven Parker
Steven Parker
229,732 Points

The instruction examples will often separate operations into individual statements for clarity. But when writing your own code, you can get the same results more compactly using techniques like method chaining. In this case you can skip the creation of "stringToShout" completely:

const shout = prompt("What do you want to shout? ").toUpperCase();

The only concern might be if this is part of a larger program and the original input is needed by code later on (or the upper-cased version in your example).

Bolivar Arguello
seal-mask
.a{fill-rule:evenodd;}techdegree
Bolivar Arguello
Front End Web Development Techdegree Student 9,429 Points

// I also did it last this and it worked.

let stringToShout = prompt("what do you want to shout?"); //let shout = stringToShout.toUpperCase(); let shoutMessage = <h2>the message to shout is: ${stringToShout.toUpperCase()}!!</h2>; console.log(shoutMessage); //alert(shoutMessage);

document.querySelector('main').innerHTML=shoutMessage;

James Summers
James Summers
704 Points

same answer as Steven, but I think it also makes the code easier to read.