Arrays

w3schools Array Reference

Declaring:

var car = new Array();
car[0] = "Mustang";
car[1] = "Challenger";

var car = ["Mustang", "Challenger"];

var car = New Array("Mustang", "Challenger");

Interating:

for(var index in cars){
    alert(cars[index]);
}

for (var i = 0; i < cars.length; i++){
    alert(cars[i]); }

cars.forEach(function(car){
    alert(car);
});

Properties

var x = cars.length; //will return 2

Methods
  *All examples are based on the original declaration of the array

var x = cars.toString(); //will return "Mustang,Challenger"
var x = cars.join(); //will return "Mustang,Challenger"
var x = cars.join('~'); //will return "Mustang~Challenger"

var langs = ["C-Sharp","Visual Basic","Java","Python"];
var x = langs.pop(); //removes "Python" which is set to x
var len = langs.push("Ruby"); //adds "Ruby" to the end of the existing array and returns the array length

var item = langs.shift(); //removes the first item of the array and assigned it to item
langs.unshift("C++"); //adds "C++" to the beginning of the array

langs.splice(2,0,"C++","Ruby"); //inserts the new elements starting at position 2, removes 0 elements
langs.splice(1,2); //starting at position 1, removes 2 elements. Result is "C-Sharp,Python"

var newarray = langs.slice(2); //slices out elements starting at position 2. Result is "Java,Python"
var newarray = langs.slice(1, 3); //slices out elements starting at position 1, up to but not including element at position 3. Result is "Visual Basic,Java"
var langs2 = ["Ruby","C++"]; var newarray = langs.concat(langs2); //combines the two arrays into one new array

delete langs[0]; //deletes the value at index 0, it will become 'undefined'

langs.reverse(); //reverses the array, it will become "Python,Java,Visual Basic,C-Sharp"
langs.sort(); //sorts the array, it will become "C-Sharp,Java,Python,Visual Basic"
Sorting is based on strings, even if your array contains numbers. So for an array with numbers with 1,2,10 were sorted, the result would be 1,10,2. To sort numbers you can do the following:
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a,b){return a-b;}); //this will sort the number array properly
points.sort(function(a,b){return b-a;}); //this will sort the number array properly in descending order

//every() checks all elements in the array against a condition, if any fail, will return false
var result = points.every(function(val){     return val > 25;
});

//some() checks all elements in the array against a condition, if any pass, will return true
var result = points.some(function(val){     return val > 25;
});

//filter checks all elements in the array against a condition, any that pass will be returned in a new array
var result = points.filter(function(val){     return val > 24;
}); //result will be 40,100,25

The find() method is not supported in IE 11 (and earlier versions).