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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Calling variables ++ ?

Hey!

I have got confused with something, but this was not mentioned on the previous videos of front end track, or perhaps I have missed something somehow?

Anyway, the teachers states the following:

document.write("<p> The random number was + randonNumber + </p>");

Instead of --> ${} :

document.write(`<p> The randon number was ${randonNumber} </p>`);

Can we use both, am I missing something or am I completely wrong?

Thank you :)))

5 Answers

Steven Parker
Steven Parker
231,268 Points

There's rarely only one way to accomplish something in programming.

The first example uses concatenation, but it's missing some quote marks. Here it is corrected:

document.write("<p> The random number was " + randonNumber + " </p>");

The second example uses a template literal to perform interpolation. Both techniques are valid, and both examples produce the same result.

Perfect, happy Coding Silvia

Thank you Harris! I am struggling a lot with JS :( I had to stop the course for a while and practise exercises, cause I am not going anywhere with this... I found this web with exercises, i add it on here in case it is any useful for you too ;) https://www.w3resource.com/javascript-exercises/javascript-conditional-statements-and-loops-exercises.php#EDITOR

To avoid concatenation errors with the + and "" it is advisable more readable and simple with template literal. Both " " + " " all works well. They both will give you the same results. For me using template literal is easier as well.

Thank you :) I find it easier and more visual template literals. And my bad, the course does explain concatenation... I just recap the basics and found out it was one of the very first thinks explained.

Hi Silva, thanks for sharing with me, I will obviously use this as well for practice exercises. Thanks again.

Thanks Silvia, I'm going to have to take a look at this, as well.

No worries, i found it really helpful, hope you do too :))))