TIL 6: javascript 2


Following on from my javascript 1 post, still refreshing the basics via freeCodeCamp.

Working with arrays:

  • arrays store several pieces of data in a single element
  • array declaration e.g. var myArr = [“some text”, 23, “hello”];
  • you can nest arrays within other arrays e.g. var ages = [[“Jon”, 28], [“Bob”, 43]];
  • access array elements using zero-based indexing, e.g. myArr[0] is “some text”
  • array entries are mutable, unlike strings
  • multidimensional arrays also use indexes to access elements e.g. ages[0][1] is 28
  • append data to the end of an array using the push() function e.g. myArr.push(“world”);
  • remove the last value on an array using the pop() function e.g. myArr.pop() would undo the above push. Further, if we’d said var removed = myArr.pop(), removed would store the value “world”.
  • .shift() removes the first element, rather than the last
  • .unshift() adds an element to the start of an array

Short one for today. Next, functions!


One response »

  1. Pingback: TIL 9: javascript 3 | diary of a scanner

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s