Javascript Built-In Object – Array

1. Create an Array:

You can create an array using either

  • an array initializer (array literal)

var myArray1 = [1,3,5,7,9];// an array with 5 elements

  • the Array constructor.

var myArray1 = new Array(1,3,5,7,9); // an array with 5 elements

2. Array Properties:

  • Array.length
  • Array.prototype

3. Methods:

  • Array.forEach()

//Loop over an Array

Example:

myArray1.forEach(function(item, index, array) {

console.log(item, index);

});

  • Array.includes()

//Determines whether an array includes a certain element

Example:

var array1 = [1, 2, 3];

array1.includes(2);

  • Array.isArray()

//Determines whether the passed value is an Array.

Example:

Array.isArray([1, 2, 3]);

  • Array.concat()

// Merge two or more arrays.

Example:

var array1 = [‘a’, ‘b’, ‘c’];
var array2 = [‘d’, ‘e’, ‘f’];

array1.concat(array2)

  • Array.find()

 // Returns the value of the first element in the array that satisfies the provided testing function.

var array1 = [5, 12, 8, 130, 44];

var found = array1.find(function(element) {

return element > 10;

});