The easiest way to add a new element to an array is using the push method:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon");          // adds a new element (Lemon) to fruits
fruits                         // ["Banana", "Orange", "Apple", "Mango", "Lemon"]

unshift() method adds a new element to an array at the beginning

New element can also be added to an array using the length property:

fruits[fruits.length] = "Lemon";     // adds a new element (Lemon) to fruits

Adding elements with high indexes can create undefined “holes” in an array.

The pop() method removes the last element from an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();              // Removes the last element ("Mango") from fruits
fruits                     // ["Banana", "Orange", "Apple"]

shift() method removes the first array element.

The Difference Between Arrays and Objects

  • In JavaScript, arrays use numbered indexes.
  • In JavaScript, objects use named indexes.

Arrays are a special kind of objects, with numbered indexes.

The JavaScript method toString() converts an array to a string of (comma separated) array values. The join() method also joins all array elements into a string:

fruits.toString();  // Banana,Orange,Apple,Mango
fruits              // Banana,Orange,Apple,Mango (automatically)
fruits.join(" * "); // Banana * Orange * Apple * Mango

The concat() method creates a new array by merging (concatenating) existing arrays:

var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias","Linus"];
var myChildren = myGirls.concat(myBoys);     // Concatenates (joins) myGirls and myBoys

The slice() method slices out a piece of an array into a new array.

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1);     // ["Orange", "Lemon", "Apple", "Mango"]
var citrus = fruits.slice(1, 3);  // ["Orange", "Lemon"]

The sort() method sorts an array alphabetically and the reverse() method reverses the elements in an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();            // Sorts the elements of fruits 
fruits.reverse();         // Reverses the order of the elements

By default, the sort() function sorts values as strings.

This works well for strings (“Apple” comes before “Banana”).

However, if numbers are sorted as strings, “25” is bigger than “100”, because “2” is bigger than “1”.

You can fix this by providing a compare function:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});  // ascending
points.sort(function(a, b){return b - a});  // descending

Sorting an Array in Random Order:

points.sort(function(a, b){return 0.5 - Math.random()});

There are no built-in functions for finding the max or min value in an array.

However, after you have sorted an array, you can use the index to obtain the highest and lowest values.

직접 선형 탐색을 통해 max/min을 찾는 것이 가장 빠른 방법이다.

Sorting an Array in Random Order:

points.sort(function(a, b){return 0.5 - Math.random()})

Yonggoo Noh

I am interested in Computer science and Mathematics.