Thomas Valentine
The JavaScript Array
February 12, 2009
|
JavaScript arrays, what could be more fun? Come along as
we cover what a JavaScript array is and when to use it.
|
Arrays
JavaScript is probably the most forgiving language in existence
when thought of in terms of data types and their conversions
from one data type to another. The JavaScript engine does
everything for you. In languages like the many flavors of C, you
have to explicitly change the type of data with a statement
before it can be used with another data type. This makes for a
very tedious job when working with more than one data type,
needlessly adding complexity.
JavaScript allows a variable to hold any data type at any time.
A situation where this might be useful is when processing user
information - the user inputs a string of numbers and you use
this input to calculate something-or-other. You would of course
use numbers in your calculation. JavaScript will automatically
change the data type from the string type the user has input
from the web page form to the Number data type so it can be used
as part of the calculation. You'll run into many instances where
the data type will be changed on the fly according to
circumstances. The change of data type, if applicable, is given
for each object, method, array, and property within the
JavaScript Language Reference.
JavaScript Arrays
An Array is used to hold a piece or many pieces of information.
In JavaScript, an array can hold one data type or all types -
JavaScript just doesn't care. In languages such as the C
flavors, an array can hold only one data type, which can cause
headaches in implementation.
The items of an array are numbered starting from the number 0
(zero) and up. There is really no limit on the size of an array.
This numbering system is called a Zero Based Index, and will be
referred to as such in the coming discussions. The arrays you'll
be working with to start with will be of the "shallow" type of
array. That is, the array is only one level deep. An array can
have an array existing within it - two arrays represented by one
name. This type of array is called a MultiDimensional Array, and
is considered to be a deep array. An array within JavaScript can
be as deep as you have a need for, with the limit being about
four levels deep (though I've personally played around with
arrays up to twelve levels deep, just for fun). This restriction
isn't due to the capabilities of JavaScript, but of the user's
computer - you have no idea what the capabilities of the user's
machine will be. Working with a very deep array may overwhelm
the user's processor. There's a lot of math involved in
accessing and working with arrays, though you don't see any of
it - it is within the computer's processor and memory.
In the beginning versions of the JavaScript language, there
wasn't a real array object to use - the closest was a string of
objects with several properties associated with them. In the
version 1.1 and up, there is a real array item that works as an
array should. Creating your array and working with it is almost
as simple as creating and working with variables - it is a
simple statement that is easy to understand.
Variables
JavaScript Introduction
Creating and Naming Arrays
The JavaScript Chronicles
JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
Part 8: Introduction to Server Side JavaScript
Part 9: Server Side JavaScript Mail Sending
Part 10: Server Side JavaScript and File Manipulation
Part 11: Working with Forms in JavaScript
Part 12: Getting to Know Dynamic HTML
|