Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 1.54 KB

File metadata and controls

55 lines (40 loc) · 1.54 KB

Do you remember how strings have special functions called methods? Don't worry if not! Here's an example to jog your memory:

$ node
> var name = "Daniel"
undefined
> name.toLowerCase()
daniel

Arrays also have several methods that you can use.

### .sort()

An array method that sorts the values in an array into ascending alphabetical or numerical order.

var unorderedLetters = ["z", "v", "b", "f", "g"];
var orderedLetters = unorderedLetters.sort();

var unorderedNumbers = [8, 5, 1, 4, 2];
var orderedNumbers = unorderedNumbers.sort();

console.log(orderedLetters); // logs [ 'b', 'f', 'g', 'v', 'z' ]
console.log(unorderedLetters); // logs [ 'b', 'f', 'g', 'v', 'z' ]
console.log(orderedNumbers); // logs [ 1, 2, 4, 5, 8 ]
console.log(unorderedNumbers); // logs [ 1, 2, 4, 5, 8 ]

When you call this array method it uses the array on the left side of the dot as an input, and it sorts that array also returning it. Note how both ordered and unordered arrays are sorted now!

.concat()

Adds (or concatenates) another value or array to the array.

$ node
> var arr = [1, 2, 3]
undefined
> arr.concat(4)
[1, 2, 3, 4]
> arr
[1, 2, 3]

Did you notice how calling the concat method did not change arr? This is because concat, like most array methods, returns a new array, it does not alter the one you called the method on.

If you wan to use the array returned by calling .concat() you should store it in a new variable.

var arr = [1, 2, 3];
var newArr = arr.concat(4);

console.log(newArr); // logs [1, 2, 3, 4]