Home » Guides Beginner Article

Learning the Basics of JavaScript

Not rated
Rate:

Amrit Hallan
November 29, 2006


Amrit Hallan
Amrit Hallan is a freelance web designer. For all web site development and web promotion needs, you can get in touch with him at http://www.bytesworth.com .
Amrit Hallan has written 1 articles for JavaScriptSearch.
View all articles by Amrit Hallan...

JavaScript is not a full-fledged programming language so you cannot churn out geeky applications with it. But yes, you can make your web site a lot more interactive and dynamic if you know how to use it to your convenience. In the initial stages, we are going to toy around with the basic syntax. Remember one thing; you can only run Javascripts if the individual browser allows them to run on an individual machine. Some web surfers disable this feature because some scripts carry viruses with them.

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:

<script language="JavaScript">

</script>

In between lies the JavaScript code. A more refined way of using such tag is:

<script language="JavaScript1.2">

</script>

As is the case with almost every software, JavaScript too has its versions - the higher the version, the more features it supports.

If you have a fair idea of how C/C++ is done, you'll find JavaScripting to be a cakewalk. Even otherwise, it is not such a big deal provided you have all your logic worked out prior to getting your hands dirty.

You can fit in a JavaScript wherever you feel like, but generally, especially if it is a collection of various functions deemed to perform various, user-action based tasks, then it's better to write the script before the <body> </body> tag to manage it well. We'll see what functions are.

For the sake of convenience, let's create a brand new HTML page. Insert the usual tags as:

<html>
<head>
</head>
<body>

</body>
</html>

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:

<script language="JavaScript">
var first_name;
var second_name;
first_name = "Amrit";
second_name = "Hallan";
alert("Hello! My name is: " + first_name + " " + second_name);
</script>

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.

Please note that every JavaScript statement ends with a semi-colon - you get an error if you omit the semi-colon. Another thing to take care of is, JavaScript is highly case-sensitive. Alert() is not same as alert(), and Amrit is not same as amrit or aMrit - they are all different in JavaScript.

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.


Add commentAdd comment (Comments: 0)  

Advertisement

Partners

Related Resources

Other Resources

arrow