Skip to content Skip to sidebar Skip to footer

Difference Between Console.log / Document.write And Alert

I would know what is the difference between those three lines of code : console.log(''); document.write(''); alert(''); (or windows.alert('');)

Solution 1:

console.log("") outputs whatever is passed as the parameter. e.g. console.log("test") will output "test" to your console

document.write("") adds whatever you want to html. e.g. document.write("<p>paragraph</p>") will add a new paragraph to a document

alert("") is a popup alert.

Solution 2:

console.log('foo');

will write 'foo' in your debugging console. You can access it via F12 on most browsers or right click on your page and inspect. You should see a "console" panel on the debugging window.

Be careful of what information you dump, it will be shown to every one browsing the page. Some browsers may not like those logs and you could encounter errors on production websites if you forget to remove them.

document.write('foo');

will append 'foo' to the DOM of your current page. This statement is not to be used for debugging purpose.

alert('foo');

will display a popup window to your browser with a single button to close it. "foo" will be the text displayed on the popup. You can use this method to send very important information to the person browsing the page, but try not to abuse of them as they block the visitor.

Solution 3:

  1. Developers use console.log() for logging useful info.
  2. document.write modifies what user sees in the browser by adding additional content to DOM.
  3. Alerts are used to alert end users who access the web page.

Solution 4:

console.log() is used by developers to just debug their code by printing the value inside console.log() in their console...... document.write() is used to add something to the webpage

Post a Comment for "Difference Between Console.log / Document.write And Alert"