console.log and document.write

Asked 1 years ago, Updated 1 years ago, 108 views

What is the difference between console.log and document.write?
Previously, I thought it would be visible or invisible in the browser, but
The browser and console had different output codes

varo={x:1,y:2,z:3};
  vara = [ ], i =1;
  for (a[i++]ino);
  console.log(a); // Output [1:x, 2:y, 3:z]
  document.write(a)//Output, x,y,z

Why is it different?

javascript console

2022-09-30 19:33

2 Answers

console.log() does not have any specific specifications or formatting.The output is useful for debugging.
document.write() adds the argument exactly as it is to HTML, and a is not a string, so it is added after stringing.


2022-09-30 19:33

document.write writes arguments to HTML on the page where the script is located.Would it be easy to understand if you just specify what to add to the HTML source code?That is, the given argument is always a String, otherwise it is rewritten and written in some form (in the example questionnaire, it is rewritten as a comma-separated string because an array has been passed).

On the other hand, console.log is a developer-oriented feature that is more of a part of the code being developed.Although most browsers print in the debug tool logs (a browser built-in tool that provides developer functionality), the debug tool's functionality determines the output format, as the details of this output are not specifically common.

For example, Google Chrome's debugging tool will output a string as a log, but if an array, object, or function is passed, it will output it so that you can see its contents.The display method is also designed to be easy to use as a debugging tool, which can be used to color-coded codes and to omit some arrays with large amounts of elements.


2022-09-30 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.