>>  Site Map >>  Forums >>  JavaScript_HTML

Forum module - topics in forum:



JavaScript_HTML - tìm hiểu và trao đỏi các mã java để làm trang web bạn sinh động hơn, các thủ thuật code html css



Javascript Array - làm việc với biến mảng

Javascript Array

An array is a variable that can store many variables. Many programmers have seen arrays in other languages and they aren't that different in Javascript
.

The following points should always be remembered when using arrays in Javascript:

* The array is a special type of variable.
* Values are stored into an array by using the array name and by stating the location in the array you wish to store the value in brackets.
Example:
myArray[2] = "Hello World";
* Values in an array are accessed with the array name and location of the value.
Example:
myArray[2];
* Javascript has built in functions for arrays, so check out these built in array functions before writing code yourself!

Creating a Javascript Array

Creating an array is slightly different than creating a normal variable. Because Javascript has variables and properties associated with arrays, you have to use a special function to create a new array. This example shows how you would create a simple array, store values to it and access these values.
Javascript Code:

Code: :
<script type="text/javascript">
<!--
var myArray = new Array();

myArray[0] = "Baseball";
myArray[1] = "Cricket";
myArray[2] = "Football";

document.write(myArray[0] + myArray[1] + myArray[2]);
//-->
</script>


Display:
Quote: :
FootballBaseballCricket


Notice that you set values and get values from an array by specifying the position, in brackets, of the value you want to use.
Javascript Array Sorting

Imagine that you wanted to sort an array alphabetically before you wrote the array to the browser. Well, this code has already been written and can be accessed by using the Array'ssort method.
Javascript Code:

Code: :
<script type="text/javascript">
<!--
var myArray = new Array();

myArray[0] = "Baseball";
myArray[1] = "Cricket";
myArray[2] = "Football";

myArray.sort();

document.write(myArray[0] + myArray[1] + myArray[2]);
//-->
</script>

Display:
Quote: :
BaseballCricketFootball


Javascript Array: Other Resources

Arrays are a very complex area of programming, especially in Javascript. This lesson could not possible cover all the areas of Javascript arrays, but we have compiled some useful resources for you to check out!

* Javascript Array Object - General information about the Javascript Array Object.
* Dev Guru Array Methods - A large collection of all the things you shouldn't program for an array because they have all been done for you!




Search from ALEXA


put your ads here