|
JavaScript Home
JavaScript TutorialIntroductionHow to Enable JavaScript JavaScript Syntax JavaScript Popup Boxes JavaScript and HTML External JavaScript File JavaScript Operators JavaScript Variables JavaScript Functions JavaScript Events JavaScript If Statements JavaScript Switch Statements JavaScript While Loop JavaScript For Loop JavaScript Try Catch JavaScript Escape Characters JavaScript Void(0) JavaScript Cookies JavaScript Date and Time JavaScript Arrays Two Dimensional Arrays JavaScript innerHTML JavaScript Summary JavaScript ReferenceJavaScript Reserved WordsJavaScript Event Handlers JavaScript Date and Time Functions JavaScript ExamplesList of ExamplesJavaScript Dropdown Menu JavaScript Print JavaScript Alert Box JavaScript Confirm JavaScript Prompt JavaScript Status Bar Messages Image Rollovers Popup Windows JavaScript Redirect Timed JavaScript Redirect JavaScript Refresh Page FREE Hosting!With every domain name you register with ZappyHost, you get FREE hosting.$1.99 Domain NamesWith every new non-domain purchase thru ZappyHost, you get a domain name for only $1.99. |
JavaScript ArraysWhat is an array?Arrays are a fundamental part of most programming languages and scripting languages. Arrays are simply an ordered stack of data items with the same data type. Using arrays, you can store multiple values under a single name. Instead of using a separate variable for each item, you can use one array to hold all of them. For example, say you have three Frequently Asked Questions that you want to store and write to the screen. You could store these in a simple variable like this:
This will work fine. But one problem with this approach is that you have to write out each variable name whenever you need to work with it. Also, you can't do stuff like loop through all your variables. That's where arrays come into play. You could put all your questions into one array. Visualizing ArraysArrays can be visualized as a stack of elements.
Note: Some languages start arrays at zero, other start at one. JavaScript arrays start at zero. Creating Arrays in JavaScriptMost languages use similar syntax to create arrays. JavaScript arrays are created by first assigning an array object to a variable name...
So, using our prior example, we could write:
Accessing Arrays in JavaScriptYou can access an array element by referring to the name of the array and the element's index number. Displaying Array Elements
The above code displays the second element of the array named faq (JavaScript array index numbers begin at zero). In this case, the value would be How to create arrays in JavaScript? Modifying the Contents of an ArrayYou can modify the contents of an array by specifying a value for a given index number:
In this case, the value of the second element of this array would now be How to modify an array? Two Dimensional ArraysSo far we've only discussed one dimensional arrays. You can also create two dimensional arrays, which can be much more powerful than one dimensional arrays. Two dimensional arrays are covered in the next lesson. Enjoy this website?
Oh, and thank you for supporting Quackit! |
Need Content for your Website?Get unique, quality digital content for your website. You can even earn money by reselling it!Includes:
|
||||||||