//STYLE: Debug with alert()

Use alert() to help debug your javascript programs

Debugging code is tricky. There are a number of tips that you can help you figure out where things break down in your code. One is the use of the alert('message'); function in Javascript.

For example, the code below makes an alert pop up after this button is clicked:

<script>        
    // after the window loads...
    window.onload   = function () {
    
        // assign an onclick event to element #buttonID
        document.getElementById('buttonID').onclick = function () {
        
            // pop up an alert box..
            alert('woot!');
            }           
    }
</script>

<body>
    <button type="button" id="buttonID">alert me!</button>
</body>

I will often insert an alert(); right before the part of my Javascript that isn't working to make sure that my code is getting to where I think it is going.

I will also use it to pop up the value of a variable - alert(someVariable); - in order to see if my calculations are working the way I want.

Inserting alerts is a simple way to track your code.