Examples of using apply function

Example 1: Append an array to another array.

1
2
3
4
5
var arr1 = [1, 2, 3];
var arr2 = [4, 5, 6];
Array.prototype.push.apply(arr1, arr2);
console.log(arr1); // [1, 2, 3, 4, 5, 6]

Example 2: Get the max/min item in an array of numbers.

1
2
3
var numbers = [12, 1, 35, 98, 34, 100];
var max = Math.max.apply(Math, numbers);
var min = Math.min.apply(Math, numbers);
0%