Lesson Four - Data types and arrays

The major background of JavaScript is its' primitive data types. JavaScript supports five different data types: integer numbers (positive and negative numbers with no decimals), floating-point numbers (positive and negative numbers with decimals including exponents), Boolean (true or false values), strings (words or sentences as text), and null (empty) values.

Integer and floating-point data types are very useful when performing calculations. Numbers like -13, 0, 6, 18, and 250 are concidered integers. Numbers like -6.16, 3.14, .0625, and 22.8 are concidered floating-point numbers. Another form of floating-point number are the exponents like 253, which stands for the number 9007199254740992.

Boolean (boo - lee - an) values are logical values of true or false. You can think of Boolean values in your programming as answering the questions "yes" or "no", or "on" or "off". With Boolean values, you are required to provide a return statement. This is useful for confirm dialog boxes.
Escape Sequence Character
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\' Single quote
\" Double quote
\\ Backslash
Strings are text that contain zero or more characters enclosed in double or single quotes. Any sentences, names, or comments can be considered a string. A string with zero characters is called an empty string. The one rule about strings is if you are enclosing text in double quotes and you want to put a word or phrase in quotes, you must use single quotes. The same goes for the opposite, if you are using single quotes, you must quote in doubles. However, there is a way you can use the same quote type within the same quote type. This is called using an escape sequence. The table on the right shows the escape sequences available for JavaScript. In addition to using escape sequences, you can also include HTML tags within the quotes. This allows you to format JavaScript text using HTML.

Now that we know about data types, let's look at how we can use them. One popular use is arrays. An array is a collection of values referenced by a single variable name. You can create an array in two ways:

variable_name = new Array(number of elements);

or

var variable = new Array(size);

You don't have to specify the size or number of elements. If you do, your array will be a set size. Let's suppose that we wanted to create an array for the days of the week. All arrays are listed with subscripts. For example, the first element of "variable_name" would be variable_name[0]. All subscripts start at zero, yet you can set [0] as a null character by starting the array at [1]. Knowing this, here is how we would set up the array for "dayOfWeek":

var dayOfWeek = new Array(7);
dayOfWeek[1] = "Sunday";
dayOfWeek[2] = "Monday";
dayOfWeek[3] = "Tuesday";
dayOfWeek[4] = "Wednesday";
dayOfWeek[5] = "Thursday";
dayOfWeek[6] = "Friday";
dayOfWeek[7] = "Saturday";

Now, let's print one of these days. To do this, you would type:

document.write(dayOfWeek[4]);

This will give you the day "Wednesday". If you used the zero element as "Sunday", then the output of the statement above would have been "Thursday".

Question: This is like Java! I remember the data types, the escape sequences, and the arrays. What else is similar?

Answer: A lot, actually. Keep in mind that they have similar names and qualities, but Java is a programming language, JavaScript is a scripting language. However, there are some more things they have in common, like expressions and operators.

Quick Links:
Have Java Problem
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips