C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Function apply() methodThe JavaScript Function apply() method is used to call a function contains this value and an argument contains elements of an array. Unlike call() method, it contains the single array of arguments. Syntaxfunction.apply(thisArg, [array]) ParameterthisArg - It is optional. The this value is given for the call to a function. array - It is optional. It is an array-like object. Return ValueIt returns the result of the calling function along provided this value and arguments. JavaScript Function apply() method ExampleExample 1Let's see an example to determine the maximum element. <script> var arr = [7, 5, 9, 1]; var max = Math.max.apply(null, arr); document.writeln(max); </script> Output: 9 Example 2Let's see an example to determine the minimum element. <script> var arr = [7, 5, 9, 1]; var min = Math.min.apply(null, arr); document.writeln(min); </script> Output: 1 Example 3Let's see an example to join arrays of same type. <script> var array = [1,2,3,4]; var newarray=[5,6,7,8] array.push.apply(array, newarray); document.writeln(array); </script> Output: 1,2,3,4,5,6,7,8 Example 4Let's see an example to join array of different type. <script> var array = [1,2,3,4]; var newarray=["One","Two","Three","Four"] array.push.apply(array, newarray); document.writeln(array); </script> Output: 1,2,3,4,One,Two,Three,Four
Next TopicJavaScript Function
|