
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the use of apply() function in JavaScript?
Traditionally we have objects which have their own unique methods and properties. Using apply() function we can construct a method that can access all the given objects combined.
Actually, this method works the same as call() function but when there is a need to pass an array like variables, apply() function comes into the picture.
In the following example, multiple elements were called individually, so there is no need to use apply() function instead call() function is used.
Example
<html> <body> <script> var obj = {num : 10}; var mul = function(i, j, k){ return this.num * i*j*k; } document.write(mul.call(obj,6,3,4)); </script> </body> </html>
Output
720
In the following example instead of individual elements, when an array is passed then call() function has returned NaN whereas apply() function returned a value. This is because the call() function cannot access an array.
Example
<html> <body> <script> var obj = {num : 10}; var mul = function(i, j, k){ return this.num * i*j*k; } var array = [6,3,4] document.write(mul.call(obj,array)); document.write("</br>"); document.write(mul.apply(obj,array)); </script> </body> </html>
Output
NaN 720
- Related Articles
- What is the use of Math.imul( ) Function in JavaScript?
- What is the use of Math.hypot() function in JavaScript?
- What is the use of JavaScript eval function?
- JavaScript Function Apply
- What is the use of return statement inside a function in JavaScript?
- What is the use of ()(parenthesis) brackets in accessing a function in JavaScript?
- How to apply bind() function in JavaScript?
- What is a JavaScript apply() Method?
- What is the difference between call and apply in JavaScript?
- What is the use of FIND_IN_SET () function in MySQL?
- What is the use of LOCATE() function in MySQL?
- What is the use of pheatmap function in R?
- How to use the apply() function for a single column in Pandas?
- What is the use of the map function in Python?
- What is the use of Atomics in JavaScript?

Advertisements