What is the difference between document.write and console.log?
What is the difference between console.log
and document.write
?
Answer 53122585548c35ca38004d89
document.write is used on an HTML
page to display text on the screen.
A more accurate and safer syntax is window.document.write( )
document.write is not recommended in modern programming because it has some issues, one of them being that it rewrites the page. It is a bit outdated. However it is still used a lot. innerHTML might be an alternative if done carefully.
console.log is not really part of the JavaScript standard. It is an addition to JavaScript when JavaScript is used on a browser, specifically on a JavaScript console.
console.log was created to make our life easier when we test our code. It logs to the screen the result we are looking for.
console.log is not really used in real life, in production code. It usually gets replaced by another command, one of them being your document.write.
Then we have return and I’m sure this question comes up so I’m going to mention it here.
return is part of JavaScript standard. It is not really designed to display data like console.log or document.write is. It is designed to return or give back a value. This value could come to the screen as we do it here on our exercises. Or it could be delivered to another script and that’s usually how return is used. return belongs to functions; we get an error if we use it outside of functions.
return gives us raw data that can be reused. It is used only inside of functions.
console.log and document.write gives us data in the form of string which only serves one purpose: to be displayed on the screen.
– hope it helps
6 comments
It does! Thanks!
verry learnfull, tnx.
Best explanation I’ve come across on console.log vs return. Thank you!
helpful, thanks!
Thanks. That’s certainly why when I code with “console.log” I get no return and with “document.write” there’s always something on my screen. TU
Roy! You answered this question well. I just came across document.write while reading online and was so curious to know what actually differentiates the three. You practically made justice to that. Thanks!!!!!!!!!!!!!!!!!
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons