|
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. A programming language that requires you to declare the data types of your variables is considered a strongly-typed programming language or static typed language. A programming language, like JavaScript, that does not require you to declare the data types of your variables is considered a loosely-typed programming language or dynamic typed language. In fact, JavaScript doesn't allow you to declare data types. The JavaScript interpreter automatically determines what type is stored in the variable and assigns the type. Integer and floating-point data types are very useful when performing calculations. Numbers like -13, 0, 6, 18, and 250 are considered integers. Numbers like -6.16, 3.14, .0625, and 22.8 are considered 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.
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); 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.
If you have a questions about any of the lessons, feel free to ask.