JavaScript Function apply() method

The apply method in JavaScript is utilized to invoke a function that includes a specific this value and an array of arguments. In contrast to the call method, apply accepts a single array of arguments.

Syntax

Example

function.apply(thisArg, [array])

Parameter

The parameter named thisArg is not mandatory. It represents the specific this value that is provided when invoking a function.

Array - This parameter is not mandatory and represents an object similar to an array.

Return Value

The function returns the outcome of the function call with the specified value and arguments.

JavaScript Function apply method Example

Example 1

Let's explore an illustration to identify the highest element.

Example

<script>

var arr = [7, 5, 9, 1];

var max = Math.max.apply(null, arr);

document.writeln(max);

</script>

Output:

Example 2

Let's consider an example to ascertain the smallest element.

Example

<script>

var arr = [7, 5, 9, 1];

var min = Math.min.apply(null, arr);

document.writeln(min);

</script>

Output:

Example 3

Let's see an example to join arrays of same type.

Example

<script>

var array = [1,2,3,4];

var newarray=[5,6,7,8]

array.push.apply(array, newarray);

document.writeln(array);

</script>

Output:

Output

1,2,3,4,5,6,7,8

Example 4

Let's examine a demonstration on how to concatenate arrays of various data types.

Example

<script>

var array = [1,2,3,4];

var newarray=["One","Two","Three","Four"]

array.push.apply(array, newarray);

document.writeln(array);

</script>

Output:

Output

1,2,3,4,One,Two,Three,Four

Input Required

This code uses input(). Please provide values below: