Sticking to the ancient HTML tradition of tags and all, here too we use tags to wake a script out of its slumber. The appropriate tag is:
For the sake of convenience, let’s create a brand new HTML page. Insert the usual tags as:
For the time being, since we are not dealing with functions, we’ll write the current scripts within <body> </body>.
Variables: Every programming language depends on variables as we depend on air to breath. Every single calculation happens with the help of variables. Variables, in computing languages, are memory addresses where you store temporary data, to use it on and off. Take for example, this small code:
first_name = "Amrit";
second_name = "Hallan";
alert("Hello! My name is: " + first_name + " " + second_name);
Don’t worry if it looks like too much of a stress. Let’s take things one by one.
var means we are trying to define a variable, and first_name means we are giving this variable the name "first_name". It’s sort of a memory address of a particular part of the hard-disk. So now, whenever we assign some value to first_name, it is going to be stored at that memory address – at that spot. If we assign a new value, the old one will be over-written. In this case, we store the value "Amrit" at the memory address that we are addressing as "first_name". The same is with second_name.
alert() is a function that pops up a message window. In programming languages, as also in mathematics, functions have parameters, and after doing this-and-that with these parameters, they return a processed result, whether you are happy about it or not. Even in this case, we are supplying the function alert() with the parameter "Hello! My name is: " + first_name + " " + second_name such that the result is, it shows the cumulative outcome in a popup window.
If "Hello! My name is: " + first_name + " " + second_name seems cryptic to you, I can fully understand your feelings. There are many kinds of variables like string variables, numeric variables, date variables, binary variables etc. Currently we are dealing with "string variables". Strings are what they are – strings. Strings of alphanumeric characters. Alpha means alphabetical, and numeric means numerical, so their combination is called alphanumeric. Now isn’t that smart?
So what the + (plus) sign does is, it concatenates two or more strings, and makes them into one string. Above, there are three strings, namely, "Hello! My name is: ", first_name that holds the strings "Amrit", and second_name that holds the string "Hallan".
Ah! I know what you are thinking. Try yourself what first_name – second_name does. Minus is not used in string concatenations.
An uncomfortable question might raise its head to challenge your expertise: Why do we enclose "Hello! My name is: " in quotes, and leave the other two in the open? That is because "Hello! My name is: " itself a string, but first_name and second_name are the VARIABLES that are holding two strings "Amrit" and "Hallan", respectively. We don’t enclose variables with quotes, but we enclose the actual strings.
Till here, things might be clear by now, so let us move to the next section, where we learn how to display the current date on your web page.