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

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

1 Like

The main difference is that document.write() inserts text into the document (the webpage in the browser window); console.log() does not. The user cannot see what is logging to the console as that is another interface, separate from the HTML/CSS environment.

document.write() is rather old school and hardly used anymore since it requires embedding in the actual page which is discouraged in this day and age. The aim is to maintain a separation of concerns, meaning no presentational elements or behaviors are to be embedded in the page, only HTML. You will likely learn all the newer techniques as you progress.

console.log() is a method of the JavaScript Console, a special object incuded in browsers for developers to use to debug and test their code in an interactive terminal. Only JS (ES) will work in the console, not HTML and not CSS.

383 Likes

Thanks for the clarification.

9 Likes

2 posts were split to a new topic: How to open html page on my computer?

Ah, alright. So it’s basically just like print() from Python.

6 Likes

Thanks for the clarification

1 Like

Great clarification. As a beginner it helped me understand that with relative ease.

2 Likes

Very helpful, thanks!

1 Like

Ok I’m completely new to coding and don’t quite understand. Would you use console.log to test a small script of code to see if it acts the way you want it to?

Ex. console.log( code you’re testing );

3 Likes

What do you mean here?

3 Likes

JavaScript is the name commonly used for the scripting language that comes with a browser. However, it is built around the specifications provided by ECMA. The name of their language is ECMAScript, the ES in ES2015, ES6, ES7, ES8, &c. In time, the name JavaScript may fall out of usage, or it may be the generic name that can refer to any ES iteration.

22 Likes

Yes. console.log() is similar to print() in python

5 Likes

Thanks, very well explained!

1 Like

Excelent, very clear

thanks for the amazing explanation, because i was also got confuse the difference. I’m learning JavaScript in ProgrammingHub and they still teach the old school document.write().

1 Like

Nothing against old school, but, really? They still teach this stuff? Is their any context as to how dated this is?


This goes all the way back to NetScape, to whom, by the way, we owe a lot of today’s browser technology.

3 Likes

Thank you, it is a very helpful and clear explanation!

console.log writes on the console whereas document.write writes on the document or the body

1 Like

easy clarification Thanks

1 Like

document.write is used when you are connecting JavaScript to HTML, and is used to write something to an HTML page. If you write document.write("ES2015") this is going to be the output:

Let me zoom in closer…
Capture

The “ES2015” text appears on the page.

console.log actually is most important when you are using vanilla JS with a console. If I write console.log("ES2015"), then the text ES2015 appears in the console.

4 Likes