
Some browsers show an icon indicating when a JavaScript error has occurred on the page. The best use of alert boxes is to use them sparingly while you're testing and debugging your script, then remove them once everything seems to be working. Too many alert boxes can be really annoying, especially for your user. Using alert() can be very helpful for isolating the source of a problem in your script, but you need to be cautious about overusing this technique. Therefore, there's probably something wrong with the link. If this alert isn't displayed when you click the link, you know the showAlert() function is never being called. So if this alert is displayed when you click the link, you know the link is working, so any problems you're experiencing are happening elsewhere in the function. The second alert() is inside the showAlert() function, but it's the very first command in that function. If it isn't triggered, your browser either doesn't support JavaScript (not likely these days), or JavaScript is disabled (check your browser's settings or preferences). If this alert is triggered, you know that JavaScript is enabled in your browser. Any JavaScript that is not contained within a function will be executed automatically when the page loads, without waiting for any events to occur. Notice that the first alert() is outside the showAlert() function. Var myText = "This can be whatever text you like!" To test whether there's something wrong with the function, you could add one or more alert() commands in strategic places, like this: If you click the link and nothing happens, what went wrong? It could be that there's a bug in the function, or it could be that the function was never triggered because there's something wrong with the link. In the previous lesson, you created a showAlert() function, and added a link to your web page that triggers that function. The alert() function can be very useful for debugging code. If you define a variable called myText, then try to execute the command alert(MyText) the script would fail because MyText doesn't exist.ĭebugging is the art of finding bugs in your code, and fixing them. That isn't the same as MyText one has a lower-case M, the other does not. In the example in the previous lesson, you created a variable called myText. You can use either single quotation marks ('.') or double quotation marks (".") but be sure every opening quotation mark is closed at the end of the string of characters. Whenever you're working with text in JavaScript (for example, when you displayed text in an alert box in the previous lesson), that text must be enclosed in quotation marks. Be sure every opening bracket has an accompanying closing bracket. If your script isn't working, check the following: Typos are a common source of bugs in JavaScript. If your JavaScript doesn't work as you expected it to, that might mean you have a bug in your code.



you will be able to tell when Javascript has triggered an error in your web browser.In this assignment, you will add some simple Javascript to one of your UNIT 5 > MODULE 2 Lesson 2: Javascript Errors and Debugging Overview
