JavaScript Tutorial

Variables

Typically, variables are declared with the var keyword.

<script type="text/javascript">
	var aVariable;
</script>

When the variable is declared, it can be assigned a value at the same time using =, the assignment operator.

<script type="text/javascript">
	var sVariable = "string of text";
	var nVariable = 10.37;
	var bVariable = false;
</script>

Data Types

There are six data types: boolean, null, number, object (covered in a later section), string and undefined.

The typeof operator determines the data type of a variable. The typeof operator returns one of these strings:


	"boolean"
	"function"
	"number"
	"object"
	"string"
	"undefined"

alert(typeof "text"); // shows "string"
alert(typeof 103); // shows "number"

Calling typeof null returns a value of "object" as null is considered to be an empty object reference.

The Undefined Type

The undefined type has only one value which is undefined. When a variable is defined without having a value assigned to it then it is undefined.


	var aVariable;
	alert(aVariable == undefined); // shows "true"

typeof returns "undefined" for both uninitialized and never defined variables.


	var aVariable;
	alert(typeof aVariable); // shows "undefined"
	alert(typeof uVariable); // shows "undefined"

The Null Type

The null type is the other data type that has only one value: null. A null value is an empty object pointer which is why typeof null returns "object".

The Boolean Type

The boolean type has only two literal values: true and false. true is not equal to 1, and false is not equal to 0. A value can be cast into a boolean value using the Boolean() casting function.

var b = Boolean(1);	 // returns true
b = Boolean(0);		 // returns false
b = Boolean(NaN);		 // returns false
b = Boolean("text");	// returns true
b = Boolean("");		// returns false
b = Boolean(null);		// returns false
b = Boolean(undefined); // returns false
b = Boolean({id:123});	// returns true

The Number Type

The maximum number value is stored in Number.MAX_VALUE while the minimum is stored in Number.MIN_VALUE. Check that a value is finite using the isFinite() function.

var integerNum = 124;		 // integer
var hexadecimalNum = 0xff;	// hexadecimal for 255
var floatNum = 0.05;		// floating point

NaN

NaN stands for Not a Number. NaN indicates when an operation intended to return a number has failed. Any operation involving NaN always returns NaN. NaN is not equal to any value, including NaN. This is why the isNaN() function exists.

alert(isNaN(NaN));	 // true
alert(isNaN(11));		// false - 11 is a number
alert(isNaN("18"));	// false - can be converted to number 18
alert(isNaN("red"));	 // true - can not be converted
alert(isNaN(true));	// false - can be converted to number 1

Number Conversions

There are three functions to convert nonnumeric values into numbers: the Number() casting function, the parseInt() function, and the parseFloat() function. The first function, Number(), can be used on any data type; the other two functions are used specifically for converting strings to numbers.

var num1 = Number("Hello");	// NaN
var num2 = Number(true);	 // 1
var num3 = Number("000100"); // 100
var num4 = Number("");		 // 0
var num1 = parseInt("400px");	// 400
var num2 = parseInt("");		 // NaN
var num3 = parseInt(22.5);	 // 22
var num4 = parseInt("50");	 // 50
var num5 = parseInt("0xff");	 // 255
var num1 = parseInt("B8", 16); // 184 - specifies radix of 16
var num1 = parseFloat("400px");	// 400 - integer
var num2 = parseFloat("0xA");		// 0
var num3 = parseFloat("22.5");	 // 22.5
var num4 = parseFloat("22.34.5");	// 22.34
var num5 = parseFloat("0908.5");	 // 908.5
var num6 = parseFloat("3.125e7");	// 31250000

The String Type

Here are the string literals:

	\n	 New line
	\t	 Tab
	\b	 Backspace
	\r	 Carriage return
	\f	 Form feed
	\\	 Backslash (\)
	\'	 Single quote (') — used when the string is delineated by single quotes.
	\"	 Double quote (") — used when the string is delineated by double quotes.
	\xnn	 A character represented by hexadecimal code nn (n is a hexadecimal digit).
	\unnnn A Unicode character represented by the hexadecimal code nnnn (n is a hexadecimal digit).

Once a string is created, its value can not change. The first string must be deleted and the variable filled with another string containing a new value to change the string held by the variable. This is demonstrated here:

{
	var str = "Riff";
	str = str + "Raff"; // "RiffRaff"
}

Converting to a String

To convert to a string use the toString() method. Every value has a toString() method except for null and undefined values. Number values can specify the radix to output the number in.

{
	var animal = "cat";
	alert(animal.toString()); // shows "cat"
	var b = false;
	alert(b.toString());		// shows "false"
	var num = 11;
	alert(num.toString());	// shows "11"
	alert(num.toString(2));	 // shows "1011"
	alert(num.toString(8));	 // shows "13"
	alert(num.toString(16));	// shows "b"
}

On null or undefined values use the String() casting function, which always returns a string regardless of the value type.

var n = null;
var u;
alert(String(n)); // shows "null"
alert(String(u)); // shows "undefined"
<< < [Page 3 of 22] > >>