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 trialGiorgi Gonadze
3,640 Pointschronology!
why do we type document.write() method at the end of the code and everything gets written on our page? why don't we type it several times after every time we change the variable? I know that it is not necessary but why? please someone explain to me :) Thank you!
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Giorgi Gonadze ! Every time you reach out and "touch" the DOM and write to it, there is a small performance cost. In this example, the performance cost would likely be minimal. But imagine that you are searching a database and it returns 1000 matches and you have code that writes that to the DOM for each match. The performance cost there would be rather hefty. Besides that, there is the notion of DRY (Don't Repeat Yourself) programming. If you wrote every time the variable changed, it wouldn't really be very DRY
Hope this helps!
Giorgi Gonadze
3,640 PointsThank you Jennifer. DRY abbreviation made it perfectly clear :)
Giorgi Gonadze
3,640 PointsGiorgi Gonadze
3,640 PointsI wrote document.write() method every time I changed variable name because I thought that if javascript code is read from left to right and from top to bottom, only last variable would be written and I wanted every value of message to be written.