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 |
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