What is the use of console.log
?
Please explain how to use it in JavaScript, with a code example.
Use console.log
to add debugging information to your page.
Many people use alert(hasNinjas)
for this purpose but console.log(hasNinjas)
is easier to work with. Using an alert pop-ups up a modal dialog box that blocks the user interface.
Edit: I agree with Baptiste Pernet and Jan Hančič that it is a very good idea to check if window.console
is defined first so that your code doesn't break if there is no console available.