JavaScript Arrays In detail – codeburst

Neeraj Dana
var array1 = [1,2,3,4];
var array1 = [1,2,3,4]; array1.push(5); //array1 = [1,2,3,4,5] array1.push(7); //array1 = [1,2,3,4,5,7] array1.push(2); //array1 = [1,2,3,4,5,7,2]
var array1 = [1,2,3,4]; array1.pop(); //returns 4, array1 = [1,2,3] array1.pop(); //returns 3, array1 = [1,2]
array1 = [1,2,3,4]; array1.shift(); //returns 1, array1 = [2,3,4] array1.shift(); //returns 2, array1 = [3,4]
var array1 = [1,2,3,4]; array1[0]; //returns 1 array1[1]; //returns 2
for ( var i=0, len=array1.length; i<len; i++ ) { console.log(array1[i]); }
var counter=0; while(counter<array1.length){ // insert code here counter++; }
while(true){ if (breakCondition) { break; } }
for ( ; 😉 { if (breakCondition) { break } }
var array1 = ['all','cows','are','big']; for (var index in array1) { console.log(index); }
for (var index in array1) { console.log(array1[index]); }
for (var element of array1) { console.log(element); } This prints out all, cows, are, and big.
var array1 = ['all','cows','are','big']; array1.forEach( function (element, index){ console.log(element); }); array1.forEach( function (element, index){ console.log(array1[index]); }) Both print all, cows, are, and big.
var array1 = [1,2,3,4]; array1.slice(1,2); //returns [2], array1 = [1,2,3,4] array1.slice(2,4); //returns [3,4], array1 = [1,2,3,4]
array1.slice(1); //returns [2,3,4], array1 = [1,2,3,4] array1.slice(1,4); //returns [2,3,4], array1 = [1,2,3,4]
array1.slice(); //returns [1,2,3,4], array1 = [1,2,3,4]
var array1 = [1,2,3,4], array2 = array1; array1 // [1,2,3,4] array2 // [1,2,3,4] array2[0] = 5; array1 // [5,2,3,4] array2 // [5,2,3,4]
var array1 = [1,2,3,4]; var array2 = Array.from(array1); array1 // [1,2,3,4] array2 // [1,2,3,4] array2[0] = 5; array1 // [1,2,3,4] array2 // [5,2,3,4]
var array1 = [1,2,3,4]; array1.splice(); //returns [], array1 = [1,2,3,4] array1.splice(1,2); //returns [2,3], array1 = [1,4]
var array1 = [1,2,3,4]; array1.splice(); //returns [], array1 = [1,2,3,4] array1.splice(1,2,5,6,7); //returns [2,3],array1 = [1,5,6,7,4]
var array1 = [1,2,3,4]; array1.splice(1,2,[5,6,7]); //returns [2,3], array1 = [1,[5,6,7],4] array1 = [1,2,3,4]; array1.splice(1,2,{'ss':1}); //returns [2,3], array1 = [1,{'ss':1},4]
var array1 = [1,2,3,4]; array1.concat(); //returns [1,2,3,4], array1 = [1,2,3,4] array1.concat([2,3,4]); //returns [1,2,3,4,2,3,4],array1 = [1,2,3,4]
var array1 = [1,2,3,4]; console.log(array1.length); //prints array1.length = 3; // array1 = [1,2,3]
function addFourNums(a, b, c, d) { return a + b + c + d; } var numbers = [1, 2, 3, 4]; console.log(addFourNums(...numbers)); // 10
var array1 = [1,2,3,4,5]; Math.max(array1); // 5 To find the minimum in an array, use this: var array2 = [3,2,-123,2132,12]; Math.min(array2); // -123