Skip to content

Instantly share code, notes, and snippets.

@nifl
Created April 3, 2014 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nifl/9959389 to your computer and use it in GitHub Desktop.
Save nifl/9959389 to your computer and use it in GitHub Desktop.
JS Arrays

Arrays

Arrays are data structures with automatically indexed positions. Array indices are zero–based.

Creating arrays

Literal notation

JSLint suggests literal notation for creating arrays and objects. Allows for initializing values.

var someArray = [1, 2, 3];
Constructor
var someArray = new Array();
  someArray[0] = 1;
  someArray[1] = 2;
  someArray[2] = 3;

Some array methods

shift() removes cell at first position and retrieves it's value.

pop() removes cell at last position and retrieves it's value.

push() adds a cell in the last postion nad enters a value.

arrayData[7] = undefined; Use undefined to create empty cells.

Join and Split

join() turns an array into a string.

split() turns a string into an array.

var fruits = ["Lemon","Apple","Orange","Peach"];

var str = fruits.join(', ');

alert(str); // "Lemon, Apple, Orange, Peach"
var fruits = "Apple,Orange,Peach";

var arr = fruits.split(',');

// arr is ["Apple", "Orange", "Peach"]

alert(arr[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment