Welcome to the world of JavaScript. This is a programming language that fits right into your HTML pages. It requires nothing special - no preparation, no nothing. Just put it right in there and if you did everything right it'll work!
Before I begin, here are some things that I'm going to be using in this lesson. The equals sign is the assignment operator. It sets the variable on the left equal to whatever is on the right. Also, everything contained in quotes are called strings. These are treated as text whether they're numbers or not. Keep this in mind - it becomes signifigant in the near future. Now, onto variables.
I love how JavaScript handles variables which are basically words that store information. You can use that word in mathematical operations, and it will use the information that that word is storing rather than the word. JavaScript variables are not as versatile as they are in C++, but they're much easier to work with due in part to that. To declare a variable in JavaScript, just type:
var variable_name;
That's it. You can put anything into that variable. Strings, integers, doubles, negative numbers, etc. They all work with this one declaration. From there, you can initialize them on that same line, or you can initialize them later. Like so!
var my_var = "two"; var my_init; var addvar; my_init = 3; addvar = my_init; my_var = my_init + addvar;
Variables in JavaScript are as simple as they look here. You can initialize them when you declare them like with my_var, or you can do it later like my_init and addvar. Also, variables aren't constrained to one data type like they are in C++. They can go from storing numbers to text to arrays and back again without missing a beat.
Arrays are almost as simple, but they can require a little more than simple variables. First off, though, arrays just store "cells" of information (like beans in a pod, where each bean holds a different piece of information). The best way to explain this is to just show you.
var value_three = "This is a string!"; var my_array = new Array(1, "string of text", value_three); var first_value = my_array[0]; var third_value = my_array[2];
When you declare an array, you don't have to assign it's values. But if you want to, the above is how you do it (my_array = new Array() ). This can be text, numbers, a variable, or a mix of the three. The first value in the parenthesis is stored in array_name[0], the second in array_name[1], third in array_name[2], and so on in that fashion. So my_array[0] equals 1, my_array[1] equals "string of text", and my_array[2] equals "This is a string!" Counting up from zero like this is known as indexing, and it was created for the sole purpose of confusing programmers =P. In case you might think so, changing value_three after declaring the array will NOT change the value of my_array[2]. So basically, you can declare an array in two ways - by simply declaring it, or initializing some of it's values, like so:
var my_array = new Array; var my_second_array = new Array(1,2,3,4,"5","6");
Now that you're familiar with how to declare arrays, let me show you how to initialize some values after having declared them. Examples first, then explanation this time:
my_array[0] = "seven"; my_array[1] = 3; my_array[16] = 46;
As you can see, you don't have to assign them in any order, nor do you have to initialize contiguous sets of values. You can skip values as see fit like above (I didn't define values 2-15). If you do that, the computer will put "undefined" into those values you left empty, should you accidentally try to use a value you haven't initialized yet. Also, if you want to know the length of your array, just put .length after the array name. This will return one greater than the last index of your array. So if the last defined value in your array is in array[12], array.length will be 13. This is because length doesn't count with indexing - it counts "normally." Here's an example of it in use.
var the_array = new Array; the_array[14] = "The last defined value!"; var array_length = the_array.length;
So array_length will equal 15, because the last defined value in the array is at index 14.