This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Animate Your Name Forum

View Course » View Exercise

598 points
5399f79d80ff33384000043d_484152091
Submitted by
L Lawiet Matsumyien
about 3 years ago

Console.log VS document.write

What is the difference between console.log and document.write? I am guessing that the former logs things to the console and the latter logs it to the document. However, after going through the codecademy JS course I haven't ever seen document.write, and the difference between a console & document were never explained.


4 votes

permalink

Hi L Lawiet, you are right on all counts and it is a great point.

console.log() is not standard JavaScript, it is an addition to the browsers and editors that offer a code Console. Developers were tired of using alert() to test scripts and someone decided to create a mechanism to replace alert().

Of course alert() still exists because it is a standard in JavaScript but at least we don't have to put up with popup boxes every time we need to see if our code does 3 x 3 and all thanks to console.log().

document.write() is one way to replace console.log() when we convert our code for production. It is recognized by HTML and by all browsers.

The full path is actually window.document.write();

Besides document.write() we also have prompt(), confirm() and one of my favorite ones: innerHTML()

As far as the exercises at Codecademy, most ( if not all) are done in the Console. Hence the reason why you don't see document.write() anywhere. Another reason you may not see document.write() in modern courses is because there are some issues with it and it may be on the way out, but it is still a useful command to know about.

Hope it makes sense!

2946 points
5533b41186f55278520000c6_295543857
Submitted by
tony de araujo
about 3 years ago

2 Comments

5399f79d80ff33384000043d_484152091 L Lawiet Matsumyien about 3 years ago

It makes perfect sense but there's one thing that was never really explained to me properly. What is a console exactly? I know what a document is, but I have no idea what a console is and how it relates to the document.

5533b41186f55278520000c6_295543857 tony de araujo about 3 years ago

Another good question! In general terms a console is the text entry and display device for system administration messages. A console application is a computer program designed to be used via a text-only computer interface. A user typically interacts with a console application using the keyboard and display screen. Nowadays all browsers have a JavaScript console for code testing and learning purposes. You can explore more about it at the following URL: http://en.wikipedia.org/wiki/Console_application