The previous program would run and produce four pop-up windows, one after the other, displaying their respective values. A glaring problem here is that we are repetitively using the same numbers in multiple places. If we had to perform these arithmetic operations on a different set of numbers, we would have had to replace them at multiple locations. To avoid this situation, we would assign those numbers to temporary storage locations; these storage locations are often referred to as variables.
//This program would alert the sum of 5 and 3; alert(5+3);
/* This program would generate two alerts, the first alert would display the sum of 5 and 3, and the second alert would display the difference of 5 and 3 */ alert(5+3); alert(5-3);
Let us continue with the program:
Now let us alter the value from
6; the amount of change that we will make here is minimal. We assign the value of
6 to our variable
a, and that takes care of the rest of the process; unlike our earlier script where changes were made in multiple locations. This is shown as follows:
Code commenting is a recurring and an extremely important step in the development life cycle of any application. It has to be used to explain any assumptions and/or any dependencies that our code contains.
var and until a value is assigned to it, the value of the variable will be implicitly set to
undefined; that value is overwritten on variable initialization.