Something unknown is doing we don't know what.” Sir Arthur Eddington
A resource for web developers a code snippet library with step by step tutorials.

a niche content web resource for geeks

       

Search Amazon

JavaScript: Arrays - for structured data storage, manipulation and retrieval

 

This page introduces the Array as an object for storing data of the same type.

 

Previous Previous

 

JavaScript: Forms Next Next

 

JavaScript code snippets listed by task and organised in a tutorial style each building on the previous example. New topics are introduced when required.


1.

Working with the JavaScript Array object.

Create an array using the 'var' statement and 'new' keyword with the Array() constructor function.

Filling array with text type data. Create a 'dense' array.

Accessing the data, changing the element entries and write the values of the elements to the page.

2.

Create an array using literal notation and assignment of literal values.

Filling the array with numeric type data.

Accessing the data, changing the element entries and write the values of the elements to the page.

3.

Examples of calling Array object methods 'push()' and 'pop()' using dot notation.

Note that these methods cause a 'state change' of the array.

Assigning values of the called methods to variables.

Write the values to the page.

3a.

Examples of calling Array object methods 'sort()', reverse()' and 'join()'.

Note that the 'sort()' and 'reverse()' methods cause a 'state change' of the array, where 'join()' does not.

Write the values to the page.

4.

Examples of using Array object property 'length' using dot notation.

Note that 'length' does not cause a 'state change' of the array.

Assigning values of the called methods and properties to variables.

Write the values to the page.

See the Array object reference for list of essential methods and properties.

5.

Create an array of a predefined size (length).

Using a 'for loop' to iterate through an 'array' structure.

Adding elements by assignment to individual array index values.

Use of \ to escape an ' character.

5a.

Experimenting with the array size (length): dynamic arrays.

Create an array of a predefined size and assign values to index values larger than the array size: this results in extending the array size to accommodate the values.

Create an array without first defining the size and assign a value to a random index value: this results in leaving the first part of the array 'empty' or 'undefined'.

 

Using a 'for loop' to iterate through an array structure and write to the page in each case.

6.

Using a 'for loop' with a 'parallel array' structure.

Use of 'parallel array' structure in storing related data.

Collecting user input with the window 'prompt()' method dialog box and storing in an array.

7.

Using a 'for in loop' with an 'associative array' structure.

Use of 'associative array' structure in storing related data.

8.

Creating a multi-dimensional array: two (2) dimensions example.

Simple notation to access the elements in a two (2) dimensional array.

Example of more efficient method of accessing the data using nested 'for loops'.

9.

Build on the above and learn about JavaScript and Forms.

View code examples on JavaScript: Forms page.

Suggest a code snippet to add to this category.

Rate this page:








Back to top.

 

Bookmark and Share

 

Copyright © 2006-2010 justfigures.co.uk