NextGen Knowledge Center

Arrays

Arrays are native objects indexed with bracket notation that can contain other objects, arrays, or primitive types. Arrays can be initialized using Java-like constructors or bracket notation and do not need to be sized upon construction:
var myArray1 = new Array(10);
var myArray2 = new Array(5, "some string", new Array());
var myArray3 = [];
var myArray4 = [ "one", "two", "three" ];

Uninitialized elements in arrays are undefined, so use the length property (var size = myArray.length;) to get a size. Use the delete operator to remove the index value, which sets the element as undefined. There are several built-in methods for arrays: concat(), reverse(), replace(), sort(), indexOf(). Arrays in JavaScript are zero-based indexed, so use myArray[0] to access the first element. Other important native objects in JavaScript include:

  • String
  • Date
  • Boolean
  • RegExp (regular expressions)
  • Math
  • XML