The alert()
function
The
alert(message) method opens a pop-up dialog with message
displayed and waits for the user to dismiss the pop-up window.
The following script:
let temp = 32.6; let beanCounter = 4; let reallyCool = true; let motto = "I Rule"; temp = (temp - 32) * 5 / 9; motto += " and so do you!"; let pos = Math.random(); // Now let's see what we've got alert("temp is: " + temp); alert("motto is: " + motto); alert("And pos is: " + pos);
Will pop-up three (really annoying) messages, one after the other. To avoid the annoyance, the alert lines have been commented out in the source.
The console.log()
function
The console.log() is a much better tool to debug your code.
Instead of the alert()
calls, you can just:
console.log("temp is: " + temp + " and motto is: " + motto); console.log("And pos is: " + pos);
Normal visitors to your site will be none the wiser, since they won't see a thing, but you, with your developer tools console loaded, will be able to see what is going on with your JavaScript.