When is JavaScript Executed?

Intro to JavaScript

When is JavaScript Executed?

🙋 Need help? Ask an expert now!

JavaScript code is executed during page loading or when the browser fires an event. All the statements in JavaScript are executed at page loading, even if most of the statements are just functions that are not used until the user interacts wit the site.

Function calls or code can be attached as "event handlers" via tag attributes that are then executed when the event is fired by the browser. For example, clicking a button could screen could trigger a piece of code in the JavaScript to do something. (i.e. change the color of the button).

The sample code above has a method called alert() that will send an alert message to the browser when it is run. If you haven't already ran the program, run it to see what happens. The alert() method will print a dialogue box with a message when called. An alert box is often used when you want the user to double check the option he/she selected before moving on.

Edit Me on GitHub!

Application Question

Which of the following alert() statements return "Beware!"?