1

I would know what is the difference between those three lines of code :

console.log("");
document.write("");
alert(""); (or windows.alert("");)
idkn
  • 422
  • 2
  • 7
  • 13
  • 3
    Just try them. You can't possibly get confused. :) – Margaret Bloom Feb 28 '16 at 13:18
  • [`document.write`](//developer.mozilla.org/en/docs/Web/API/Document/write) is _very_ misunderstood and none of the answers below got it quite right. `document.write` does one of two things: it either writes data into the _current open stream_ (i.e. literally the data stream of HTML source that the browser is downloading), or it _opens a new stream, discarding the result of the old one_, and _then_ writes into it. These writing operations are in-place. This only makes sense if JS is executed while _blocking the HTML parser_, which _shouldn’t happen anyway_ (in 2022, modules are preferred). – Sebastian Simon Dec 19 '22 at 21:25

4 Answers4

3

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
RinaLiu
  • 400
  • 2
  • 9
  • _“`console.log("")` outputs whatever is passed as the parameter”_ — So do the other two. There are several other significant differences and this answer lacks a _lot_ of nuance. – Sebastian Simon Dec 19 '22 at 21:27
3
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 by requiring to dismiss the modal before doing anything else.

Tim
  • 1,238
  • 1
  • 14
  • 24
  • `alert` doesn’t block the visitor. This is not what “blocking” means. `alert` blocks the current JavaScript execution until the `alert` window is closed. – Sebastian Simon Dec 19 '22 at 21:20
0
  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.
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
0

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

Avishkar
  • 1
  • 1