0% found this document useful (0 votes)
18 views

Javascript Array

Javascript array is a variable that can store many variables within it. Arrays are a very complex area of programming, especially in javascript. Check out these useful resources before writing your own array code.

Uploaded by

Minch Cerrero
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Javascript Array

Javascript array is a variable that can store many variables within it. Arrays are a very complex area of programming, especially in javascript. Check out these useful resources before writing your own array code.

Uploaded by

Minch Cerrero
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

javascript array

An array is a variable that can store many variables within it. Many programmers have seen arrays in other languages, and they aren't that different in JavaScript.
Advertise on Tizag.com

The following points should always be remembered when using arrays in JavaScript: y y y y 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 by 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 the code yourself!

creating a javascript array


Creating an array is slightly different from 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:
<script type="text/javascript"> <!-var myArray = new Array(); myArray[0] = "Football"; myArray[1] = "Baseball"; myArray[2] = "Cricket"; document.write(myArray[0] + myArray[1] + myArray[2]); //--> </script>

Display:
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's sort method.

JavaScript Code:

<script type="text/javascript"> <!-var myArray2= new Array(); myArray2[0] = "Football"; myArray2[1] = "Baseball"; myArray2[2] = "Cricket"; myArray2.sort(); document.write(myArray2[0] + myArray2[1] + myArray2[2]); //--> </script>

Display:
BaseballCricketFootball

javascript array: other resources


Arrays are a very complex area of programming, especially in JavaScript. This lesson cannot possibly cover all the areas of JavaScript arrays, but we have compiled some useful resources for you to check out! y y
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!

You might also like