简体   繁体   中英

Console.log vs document.write

When using JavaScript in HTML, console.log(); is replaced with document.write(); . But console.log(); is still perfectly usable, but I don't exactly know when it would be used, seeing as it doesn't print anything. And no site I've visited has had any real information about this.

document.write("Thanks!!");

console.log logs to the console . If you use Chrome, press F12 and find it there. document.write writes to the document, ie to your HTML page. document.write is almost never used nowadays, because it replaces the whole page with what you write, and this is not much useful.

This string will be placed on the website.

document.write("Thanks!!");

This string will be placed in the browser console (pres F12 to toggle console)

console.log("Thanks!!");

 document.write("Thanks!!"); console.log("Thanks!!"); 

console.log is used to write logs to the console, mainly for debugging purposes https://developer.mozilla.org/en-US/docs/Web/API/Console/log

document.write is used to write markup to the document, and if executed after page load it replaces everything in the header and body tag with the markup given to the method. https://developer.mozilla.org/en-US/docs/Web/API/Document/write

I think you are new to Java Script. console.log() is used to for debugging purpose. While document.write() is used when you want some thing to be written on DOM of browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM