Get and set array values

To get and set array values, you use square brackets and provide the zero-based numeric index:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var colors = ["A", "B", "C"]; //define an array of strings 
        document.writeln(colors[0]); //A,display the first item 
        colors[2] = "black";       //change the third item 
        document.writeln(colors[2]); //,display the third item 
        colors[3] = "brown"; //add a fourth item 
        document.writeln(colors[3]); //display the fourth item 
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The following code changes the first element in the array:

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
  <script type="text/javascript">
    var myArray = [ 100, "Adam", true ];
    myArray[0] = "Tuesday";
    document.writeln("Index 0: " + myArray[0]);
  </script>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Essential Types  

Array:
  1. The Array Type
  2. Array Built-in Methods
  3. Detecting Arrays
  4. Get and set array values
  5. Enumerating the Contents of an Array
  6. Array Length
  7. Array join() method
  8. Array concat()
  9. Array indexOf()
  10. Array lastIndexOf()
  11. Array every()
  12. Array filter() filters array with the given function.
  13. Array map()
  14. Array forEach()
  15. push() and pop():Array Stack Methods
  16. push(), shift():Array Queue Methods
  17. Array reduce()
  18. Array reduceRight()
  19. reverse():Reordering array
  20. Array slice()
  21. Array some()
  22. Array splice()
  23. Array sort()
  24. toString(), toLocaleString() and valueOf Array
  25. Array unshift()