Note: Make sure the <script> comes right before the closing </body> tag! This ensures that the rest of the document elements have been loaded before the script is run.
Example: If we're programming a game, a variable could keep track of the player's current score.
Example: If we're programming a grocery list app, then we could use an array to keep track of all the grocery items that the user wants to buy.
Example 1: In Step 0, you used the alert(...) function without needing to the details of how it was implemented!
Example 2: In my game, if I needed to calculate the distance between two points in multiple places in my code, I can write a distance function that takes the coordinates of the two points as the inputs and returns the calculated distance. This way, I only have to write the distance code once, but I can call the distance function in multiple places.
Example: If our "score" variable is less than or equal to 0, then tell the user "You Lost!".
Example: We could use a for-loop in our grocery list app to send a reminder to the user for each item on their list. The number of repetitions is the number of items on their grocery list.
The notation that we use to call a function that "belongs" to some object is myObject.functionName(...).
The notation that we use to get the value of an object's property is myObject.propertyName.
Debugging tip: If your code is not returning the correct HTML elements, make sure the <script> element in your index.html file comes right before the closing </body> tag. This ensures that the other document elements have been loaded before the script is run.