Skip to content

Instantly share code, notes, and snippets.

@Ingelheim
Created August 25, 2016 11:14
Show Gist options
  • Save Ingelheim/0feb72bdc60c4a4820889ff228e48cc3 to your computer and use it in GitHub Desktop.
Save Ingelheim/0feb72bdc60c4a4820889ff228e48cc3 to your computer and use it in GitHub Desktop.
function createArray(w, x, y, z) {
  return [w, x, y, z]
}

createArray(1, 4, 6, 3) // [1, 4, 6, 3]
createArray(1, 2, 3, 4) // [1, 2, 3, 4]
createArray("hello", "bye", "test", "morning") // ["hello", "bye", "test", "morning"]

var testArray = [1, 2, 3, 4, 5]
// LENGTH of 5
// has 5 ELEMENTS
// INDEX 0 holds ELEMENT 1 
// INDEX 4 holds ELEMENT 5

// RETRRIEVE AN ELEMENT (e.g. 3)
testArray[2] // -> 3

// Set an ELEMENT
testArray[2] = 6// -> [1, 2, 6, 4, 5]

// Append / add / push new element to array
testArray.push(9) // -> [1, 2, 6, 4, 5, 9]

// Prepend / add new element to array (unshift)
testArray.unshift(0) // -> [0, 1, 2, 6, 4, 5, 9]

for(var i = 0; i < testArray.length; i++) {
  var element = testArray[i];
//   do stuff with the element
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment