Arrays: Simple JavaScript Code

Topics

Javascript 

   arrays
      collection of values 
      single name
      indexed
   setting a value
   getting a value
   adding to the array
   deleting from the array
   
   objects
   
   loops  
   
   

<!DOCTYPE html>
<!-- FILE: arrays.html -->
<!-- wyatt -->
<html>
<head> 
    <title>arrays: how to write js code</title>
    <meta charset="UTF-8" />

    <script>
            ** SEE JS BELOW **
    </script>            

    <style>    
            ** SEE CSS BELOW **       
    </style> 
    
</head>
<body>
   

JS Arrays

The horizontal rule below ends the html in this file. Everything below that line is created "on the fly" by javascript...


</body> </html>

// wyatt
// assign values and observe
function messWithArrays()
{
   // declare an array - has no elements
   var  myArray=[]; 
   
   // length and element
   document.write('

myArray.length is: ' + String(myArray.length) + ' and the type is ' + typeof(myArray.length) + '

'); document.write('

myArray[0] is: ' + String(myArray[0]) + ' and the type is ' + typeof(myArray) + '

'); myArray[0] = 99; // put a single element into the array // length and element document.write('

myArray.length is: ' + String(myArray.length) + ' and the type is ' + typeof(myArray.length) + '

'); document.write('

myArray[0] is: ' + String(myArray[0]) + ' and the type is ' + typeof(myArray[0]) + '

'); myArray[1] = 521; // put a single element into the array // length and element document.write('

myArray.length is: ' + String(myArray.length) + ' and the type is ' + typeof(myArray.length) + '

'); document.write('

myArray[1] is: ' + String(myArray[1]) + ' and the type is ' + typeof(myArray[1]) + '

'); myArray[myArray.length] = 11; // put a single element into the array // length and element and element? document.write('

myArray.length is: ' + String(myArray.length) + ' and the type is ' + typeof(myArray.length) + '

'); document.write('

myArray[2] is: ' + String(myArray[2]) + ' and the type is ' + typeof(myArray[2]) + '

'); document.write('

myArray[myArray.length] is: ' + String(myArray[myArray.length]) + ' and the type is ' + typeof(myArray[myArray.length]) + 'WHY??

'); // loop through the entire array document.write('

myArray.length is: ' + String(myArray.length) + '

'); var counter=0; while(counter< myArray.length) { document.write('

myArray[' + counter + ']; is: ' + String(myArray[counter]) + ' and the type is ' + typeof(myArray[counter]) + '

'); counter++; } }

/*
wyatt
*/
   /* css rule */
   body /* tag selector */
   {
     background-color:white; /* property:value */
   }
   .sample /* class selector */
   {
     background-color:#ddd;  /* property:value */
     color:red;   /* property:value */
     padding:20px;
   }