The Array Type

JavaScript arrays are ordered lists of data. JavaScript arrays can hold any type of data in each slot. JavaScript arrays are dynamically sized.

Arrays can be created in two ways. The first is to use the Array constructor:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var names = new Array(); 
        document.writeln(names.length);//0
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

You can pass the number of item into the constructor, and the length property is set to that value.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var names = new Array(20); 
        document.writeln(names.length);//20
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The Array constructor can accept array items:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var names = new Array("A", "B", "C"); 
        document.writeln(names.length);//3
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

It's possible to omit the new operator when using the Array constructor.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var names = Array("A", "B", "C"); 
        document.writeln(names.length);//3
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Create an array with array literal notation

The second way to create an array is by using array literal notation.

The following code creates an array with three strings:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var colors = ["red", "blue", "green"]; //creates an array with three strings 
        document.writeln(colors.length);//3
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The following code creates an empty array:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var names = []; //creates an empty array 
        document.writeln(names.length);//0
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The following code populates data with different type into an array.

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
  <script type="text/javascript">
    var myArray = new Array();
    myArray[0] = 100;
    myArray[1] = "Adam";
    myArray[2] = true;
  </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()