Profile image of LDeen
Submitted by LDeen
over 11 years

What is the difference between document.write and console.log?

What is the difference between console.log and document.write?

Answer 53122585548c35ca38004d89

17 votes

Permalink

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

Profile image of tarau
Submitted by tarau
over 11 years

6 comments

Profile image of LDeen
Submitted by LDeen
over 11 years

It does! Thanks!

Profile image of callme
Submitted by callme
almost 11 years

verry learnfull, tnx.

Profile image of FutureCodekage
Submitted by FutureCodekage
over 10 years

Best explanation I’ve come across on console.log vs return. Thank you!

Profile image of Elishala
Submitted by Elishala
over 10 years

helpful, thanks!

Profile image of axel8591
Submitted by axel8591
about 10 years

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

Profile image of ThirdSamuel
Submitted by ThirdSamuel
about 10 years

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!!!!!!!!!!!!!!!!!