
- 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
JavaScript Function Apply
The JavaScript apply() function allows us to use the same method from different objects. The parameters are passed as an array here.
Following is the code for the JavaScript function apply() −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; } </style> </head> <body> <h1>JavaScript apply()</h1> <div class="sample"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above buttons to check if adult or not </h3> <script> let sampleEle = document.querySelector(".sample"); let obj1 = { name: "Rohan", age: 22 }; function checkAdult(vehicle, country) { if (this.age > 18) { sampleEle.innerHTML =this.name +" you are an adult and can drive " + vehicle + " in " + country; } else sampleEle.innerHTML =this.name + " you are an adult and cannot drive " + vehicle + " in " + country; } document.querySelector(".Btn").addEventListener("click", () => { checkAdult.apply(obj1, ["car", "INDIA"]); }); </script> </body> </html>
Output
On clicking the click here button −
- Related Articles
- How to apply bind() function in JavaScript?
- What is the use of apply() function in JavaScript?
- How to apply function against an accumulator and each key of object in JavaScript?
- What is a JavaScript apply() Method?
- Apply function to every row in a Pandas DataFrame
- How do I apply some function to a Python meshgrid?
- Apply the ufunc outer() function to all pairs in Numpy
- Invoking functions with call() and apply() in JavaScript
- Apply function to every row in a Pandas DataFrame in Python
- How to apply rectified linear unit function element-wise in PyTorch?
- How can we apply an anonymous function to the pandas series?
- How to Apply the Reverse Find or Search Function in Excel?
- How to apply EXTRACT() function on the dates stored in MySQL table?
- How can we apply COALESCE() function on a MySQL table’s data value?
- How to use the apply() function for a single column in Pandas?

Advertisements