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 trialSilvia Ramos
Front End Web Development Techdegree Graduate 18,318 PointsCalling 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
231,236 PointsThere'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.
Harris Keka
Courses Plus Student 3,611 PointsPerfect, happy Coding Silvia
Silvia Ramos
Front End Web Development Techdegree Graduate 18,318 PointsThank 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
Harris Keka
Courses Plus Student 3,611 PointsTo 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.
Silvia Ramos
Front End Web Development Techdegree Graduate 18,318 PointsThank 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.
Harris Keka
Courses Plus Student 3,611 PointsHi Silva, thanks for sharing with me, I will obviously use this as well for practice exercises. Thanks again.
Alex Smith
5,133 PointsThanks Silvia, I'm going to have to take a look at this, as well.
Silvia Ramos
Front End Web Development Techdegree Graduate 18,318 PointsNo worries, i found it really helpful, hope you do too :))))