Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
49 views

Java Script Arrays

The document discusses arrays in JavaScript. It explains that an array is a variable that can store multiple values, unlike regular variables which store a single value. The document demonstrates how to define an array, access elements within an array using indexes, add and remove elements, and use various array methods like pop, push, shift, unshift, splice, and slice.

Uploaded by

mihaelahristea
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Java Script Arrays

The document discusses arrays in JavaScript. It explains that an array is a variable that can store multiple values, unlike regular variables which store a single value. The document demonstrates how to define an array, access elements within an array using indexes, add and remove elements, and use various array methods like pop, push, shift, unshift, splice, and slice.

Uploaded by

mihaelahristea
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Arrays

Let's assign some string values to some variables.


var city0 = "Atlanta";
var city1 = "Baltimore";
var city2 = "Chicago";
var city3 = "Denver";
var city4 = "Los Angeles";
var city5 = "Seattle";
The variable names are all the same, except they end in different
numbers. I could have
named the variables buffy, the, vampireSlayer, and so on if I'd wanted
to, but I chose to
name them this way because of where this discussion is going.
Now, having made these assignments, if I code...
alert("Welcome to " + city3);
...an alert displays saying, "Welcome to Denver".
I'm going to show you another type of variable, one that will come in
handy for many
tasks that you'll learn about in later chapters. I'm talking about a
type of variable called an
array. Whereas an ordinary variable has a single value assigned to
it—for example, 9 or
"Paris"—an array is a variable that can have multiple values
assigned to it. You define an
array this way:
var cities = ["Atlanta", "Baltimore", "Chicago", "Denver", "Los Angeles", "Seattle"];
In the example at the beginning of this chapter, I ended each
variable name with a number.
city0 was "Atlanta", city1 was "Baltimore", and so on. The array I just
defined is similar,
but in the case of an array defined the way I just defined one,
JavaScript numbers the different
values, or elements, automatically. (You can control the numbering
yourself by defining
elements individually. See below.) And you refer to each element by
writing the array name
—cities in this case—followed by a number enclosed in square
brackets. cities[0] is
"Atlanta", cities[1] is "Baltimore", and so on.
Because JavaScript automatically numbers array elements, you
have no say in the
numbering. The first element in the list always has an index of 0,
the second element an index
of 1, and so on.
This is the alert I coded above, but now specifying an array element
instead of an
ordinary variable.
alert("Welcome to " + cities[3]);
An array can be assigned any type of value that you can assign to
ordinary variables. You
51
can even mix the different types in the same array (not that you
would ordinarily want to).
var mixedArray = [1, "Bob", "Now is", true];
In the example above, mixedArray[0] has a numerical value of 1,
mixedArray[1] has a
string value of "Bob", and so on.
Things to keep in mind:
The first item always has an index of 0, not 1. This means that if the
last item in the list
has an index of 9, there are 10 items in the list.
The same naming rules you learned for ordinary variables apply.
Only letters, numbers, $
and _ are legal. The first character can't be a number. No spaces.
Coders often prefer to make array names plural—cities instead of
city, for example—
since an array is a list of things.
Like an ordinary variable, you declare an array only once. If you
assign new values to an
array that has already been declared, you drop the var.
16
Arrays:
Adding and removing elements
As you learned in earlier chapters, you can declare an empty
variable, one that doesn't
have a value. Then you can assign it a value whenever you like.
And you can change its value
at will. You can do all these things with an array, as well.
This is how you declare an empty array.
var pets = [];
Assume that the array pets has already been declared. This is how
you assign values to
it.
1 pets[0] = "dog";
2 pets[1] = "cat";
3 pets[2] = "bird";
In the example above, I defined the first three elements of the array,
in order. But you can
legally leave gaps in an array if you choose to (not that you
normally would). For example,
suppose you start with the same empty array and code these lines.
1 pets[3] = "lizard";
2 pets[6] = "snake";
Now, if you refer to pets[3], you'll get "lizard". If you refer to pets[6],
you'll get
"snake". But if you refer to pets[0] through pets[2] or pets[4] or pets[5],
you'll get
undefined.
You can assign additional values to an array that already has
values. Assume that the first
three elements of the array pets are "dog", "cat", and "bird". Then
you write this code.
1 pets[3] = "lizard";
2 pets[4] = "fish";
3 pets[5] = "gerbil";
4 pets[6] = "snake";
Now the array has 7 elements: "dog", "cat", "bird", "lizard", "fish",
"gerbil", and "snake".
If you assign a new value to an array element that already has one,
the old value is
replaced by the new one.
Using the keyword, pop, you can remove the last element of an
array.
Suppose you have an array, pets, whose elements are "dog", "cat",
and "bird". The
following code deletes the last element, "bird", leaving a two-
element array.
54
pets.pop();
Using the keyword, push, you can add one or more elements to the
end of an array.
Suppose you have that same array consisting of "dog", "cat", and
"bird". The following
code adds two new elements to the end of the array.
pets.push("fish", "ferret");

17
Arrays:
Removing, inserting,
and extracting elements
Use the shift method to remove an element from the beginning of an
array.
Suppose you have an array, pets, whose elements are "dog", "cat",
and "bird". The
following removes the first element, "dog", leaving you with a two-
element array.
pets.shift();
To add one or more elements to the beginning of an array, use the
unshift method. The
following code adds two elements to the beginning of the array.
pets.unshift("fish", "ferret");
Use the splice method to insert one or more elements anywhere in
an array, while
optionally removing one or more elements that come after it.
Suppose you have an array with
the elements "dog", "cat", "fly", "bug", "ox". The following code adds
"pig", "duck", and "emu"
after "cat" while removing "fly" and "bug".
pets.splice(2, 2, "pig", "duck", "emu");
The first digit inside the parentheses is the index of the position
where you want to start
adding if you're adding and deleting if you're deleting. The second
digit is the number of
existing elements to remove, starting with the first element that
comes after the element(s) that
you're splicing in. The code above leaves you with an array
consisting of "dog", "cat", "pig",
"duck", "emu", and "ox".
You could make additions without removing any elements. The
following code adds
"pig", "duck", and "emu" without removing any elements.
pets.splice(2, 0, "pig", "duck", "emu");
You can also remove elements without adding any. If you start with
the elements "dog",
"cat", "fly", "bug", and "ox", the following code removes two
elements starting at index 3
—"bug" and "ox". This leaves "dog", "cat", and "fly".
pets.splice(2, 2);
Use the slice method to copy one or more consecutive elements in
any position and put
them into a new array. If you start with an array, pets, consisting of
"dog", "cat", "fly", "bug",
57
and "ox", the following code copies "fly" and "bug" to the new array
noPets and leaves the
original array, pets, unchanged.
var noPets = pets.slice(2, 4);
The first digit inside the parentheses is the index of the first element
to be copied. The
second digit is the index of the element after the last element to be
copied.
Two things could trip you up here:
Since the first index number inside the parentheses specifies the
first element to be
copied, you might think the second index number specifies the last
element to be copied.
In fact, the second number specifies the index number of the
element after the last element
to be copied.
You must assign the sliced elements to an array. It could, of course,
be the same array
from which you're doing the slicing. In that case, you'd be reducing
the original array to
only the copied elements.

You might also like