Javascript Arrays and Loops (iteration)

A quick list-style reference to JS arrays and loops.

Arrays

Note: For associative arrays use "Object" in stead of "Array"

Declare array
var my_rray = [];
var my_rray = new Array(); (keyword "new" is optional)
Create array
var my_rray = ["foo", "bar"];
var my_rray = new Array("foo", "bar"); (keyword "new" is optional)
var my_rray = new Array(); (keyword "new" is optional)
var my_rray = my_string.split(",");
var my_rray = my_string.match("RegExp");
var my_rray = array1.concat(array2 [, ...]);
var my_rray = array1.slice(start [, end]);
Check array
var isThisAnArray = Array.isArray(my_rray); ("Array." optional)
var howManyElements = my_rray.length;
Get item value
var firstElement = my_rray[0];
var lastElement = my_rray[my_rray.length - 1];
Get index value
var x = my_rray.indexOf("foo" [, start]);
var x = my_rray.findIndex(function("foo" [, index [, array]]) [, thisValue]); (link)
var x = my_rray.lastIndexOf("foo" [, start]);
Array to string
var x = my_rray.join(",");
var x = my_rray.toString();
var x = my_rray.valueOf();
Operations
my_rray.copyWithin(target, start [, end]); (link)
my_rray.fill(value [, start [, end]]);
my_rray.reverse();
my_rray.sort([function(){}]); (link)
Add to array
my_rray.push("foo" [, ...]);
my_rray['index'] = "foo"; ('index' must be a number)
my_rray += "foo";
Add to first place
var x = my_rray.unshift("foo" [, ...]);
Add to last place
var x = my_rray.push("foo" [, ...]);
Remove first element(s)
var x = my_rray.shift();
Remove last element(s)
var x = my_rray.pop();
Add or remove by index value
var x = my_rray.splice(index , number [, item1 [, ...]]); (link)

Array Iteration: Loops / Search

Note: these all use callback functions.

"Every" (link)
my_rray.every(function("foo" [, index [, array]]) [, thisValue]);
"ForEach" (link)
my_rray.forEach(function("foo" [, index [, array]]) [, thisValue]);
"Filter" (link)
my_rray.filter(function("foo" [, index [, array]]) [, thisValue]);
"Find" (link)
my_rray.find(function("foo" [, index [, array]]) [, thisValue]);
"Map" (link)
my_rray.map(function("foo" [, index [, array]]) [, thisValue]);
"Reduce" (link)
my_rray.reduce(function(total, currentValue [, currentIndex [, array]]) [, initialValue]);
"ReduceRight" (link)
my_rray.reduceRight(function(total, currentValue [, currentIndex [, array]]) [, initialValue]);

General Javascript Iteration / Loops

"Switch" (link)
switch(expression) {
  case x:
    // code here...
    break;
  case y:
    // code here...
    break;
  default:
    // code here...
}
"For" (link)
for (let i = 0; i < object.length; i++) {
  // code here...
}
"For ... in" (link) (not recommended for arrays)
for (i in object) {
  // code here...
}
"For ... of" (link)
for (let i of iterable) {
  // code here...
}
"For await ... of" (link) (async only)
for await (let i of iterable) {
  // code here...
}
"While" (link)
while (condition) {
  // code here...
}
"Do ... while" (link)
do {
  // code here...
} while (condition)

Control Flow

"If ... else" (link)
if (condition) {
   // code here...
} else if {
   // code here...
} else {
   // code here...
}
"Try ... catch" (link)
try {
   // code here...
} catch (exception_var) {
   // code here...
} finally {
   // code here...
}

Control Statements