+ 1
What is console.log(*) used for in JavaScript
I didn't come across it in the js tutorial but am seeing frequently now in examples
6 Réponses
+ 7
I suppose it depends on what you mean by purpose. If you are using document.write() for debugging purposes, then yes. However, document.write() obviously works differently from console.log() in that one affects the page while the other does not.
Sometimes, you'll want to output information that will present better in console.log(). A few examples are:
- Serialized JSON objects
- Sequence of actions with timestamps posted for each step or iteration in a looping construct.
It should also be noted that console.log() will not require HTML for simple formatting like line breaks as document.write() does.
I almost rarely use document.write() for debugging purposes. Console.log() is more appropriate in that is doesn't impact the UI.
+ 5
console.log() is used for logging from your code to the console window, which is a panel in your browser development tools.
This is a great way to log code activity for debugging purposes.
+ 2
David Carroll what I meant.
+ 1
Does it serve the same purpose as document.write. if no, which should be used in the actual coding process
+ 1
You can use console.log to write to the log file. If your browser supports debugging, you can use theconsole.log method to displayJavaScript
+ 1
Markpeach96 I wasn't aware that console.log() could be used to write to a file. Did you mean to say write to console?