!!! JavaScript

The JavaScript language is a general purpose language that is most often, but not necessarily, run within an Internet browser.  Except for the name, JavaScript is completely unrelated to Java.

!! The Language

! Data types


All numbers are floating point using a format that exactly represents integers (no rounding error for integers).

No character type

Strings are represented with double-quotes OR single-quotes, i.e.

{{{"Hello"
'there'}}}

Single-quote strings can embed double-quotes, and double-quote strings can embed single-quotes, i.e.:

{{{"don't you like this string"
'var abc = "value"'}}}

You can also use escapes "\", i.e.:

{{{'don\'t you like this'}}}

You can Concatenate strings with +, i.e.:

{{{"abc" + "def"}}}


! Boolean values and Null

Two constants "true" and "false"

Result of comparisons, i.e.:

{{{x == y}}}

"null" is also a constant that means no value

! Variables


Variables are type-less.  You can assign a number, string, etc. to any variable at any time.

Declare with "var" like this:

{{{var a;
var b = 44;
var c = 'hello';
var d, e=7, f;}}}

Be very careful!  Most JavaScript's will allow you to assign a value to an undeclared variable.  It will seem to work but you have unknowingly created a GLOBAL variable rather than one local to the function you are in.  ALWAYS declare variables!

Variables are local to the context in which they are declared.  This means if you declare it outside of a function, it is global.  If you declare it inside a function it is local to that function.  Keep all variables as local as possible.


! Functions

There are two ways of declaring a function as follows ("function" is a keyword):

{{{function myFunc(x, y) {
	return x + y;
}
}}}

The above is pretty traditional.  Notice that none of the variables have types.  Elsewhere, you can call the above function in an expected way, i.e.:

{{{myFunc(3, 4);}}}

Or, to use the return value:

{{{var r = myFunc(3, 4);}}}

Another way to create a function is through a variable syntax.  Although you may think "what good is that?", it is extremely important, and it opens the door to some very, very important things.  The above function can also be defined as follows:

{{{var myFunc = function(x, y) {
	return x + y;
}
}}}

This makes the following about JavaScript clear:

1.  All functions in JavaScript are just variables holding function values!

2.  Function values can be passed around and assigned just like variables, i.e.:

{{{function abc(x, y) {
	return x + y;
}

var def = function(x, y) {
	return x * y;
}

print(abc(3, 4));
print(def(3, 4));

var g = abc;
var h = def;

print(g(4, 5));
print(h(4, 5));
}}}

So, it doesn't matter which way you declare the function, it is the same.  The value of declaring it as a variable, however, is to make it clear what is really is - a variable!

Inside function are all of the standard stuff like "if", "else", "while", "do", "for", "return", "switch", "continue", "break", "throw", "try/catch", "with", etc..


! Arrays

Arrays in JavaScript are zero based indexing.

{{{var myArray = [36, 45, 'hello'];

print(myArray[0]);
print(myArray[1]);
print(myArray[2]);

myArray[1] = 'dog';

print(myArray[0]);
print(myArray[1]);
print(myArray[2]);

print(myArray.length);

// You can auto-extend an array:

myArray[3] = 'new element';

print(myArray[3]);
print(myArray.length);

myArray[43] = 'far away';
print(myArray[43]);
print(myArray.length);  // returns 44}}}


! Object

Objects are really hash tables!


{{{var x = new Object();

x['George'] = 44;  // key=George, value=44
x['sam'] = 27;
x['marry'] = 33;

print(x['George']);
print(x['sam']);

// alternate way of doing the same thing

var x = new Object();

x.George = 55;  
x.sam = 66;
x.marry = 77;

print(x.George);
print(x.sam);

// you can even assign them one way and access them in the other


print(x['George']);
print(x['sam']);

print(Object.keys(x).length);  // the number of elements

// Enumerating through all the keys

for (var k in x)
    print(k);


// Enumerating through all the values

for (k in x)
    print(x[k]);


// A very important way of initializing an object:

var y = { abc: 104, def: 5.5, hij: 'hello' };

print(y["abc"]);
print(y.def);
print(y.hij);
}}}