TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Function apply() Method

JavaScript Function apply() Method with example on javascript function object, javascript tutorial, apply(), bind(), call(), toString(), toSource(), isgenerator(), javascript handler, javascript reflect, object, number etc.

<< Back to JAVASCRIPT

JavaScript Function apply() method

The 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.

Syntax

function.apply(thisArg, [array])

Parameter

thisArg - 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 Value

It returns the result of the calling function along provided this value and arguments.

JavaScript Function apply() method Example

Example 1

Let'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>
Test it Now

Output:

9

Example 2

Let'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>
Test it Now

Output:

1

Example 3

Let'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>
Test it Now

Output:

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

Example 4

Let'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>
Test it Now

Output:

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





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf