- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to define a method in JavaScript?
A method in JavaScript is the action performed on objects. A JavaScript method has a function definition, which is stored as a property value.
Example
Let’s see an example to define a method in JavaScript
<!DOCTYPE html> <html> <body> <h3 id="myDept"></h3> <script> var department = { deptName: "Marketing", deptID : 101, deptZone : "North", details : function() { return "Department Details<br>" + "Name: " + this.deptName + " <br>Zone: " + this.deptZone + "<br>ID: " + this.deptID; } }; document.getElementById("myDept").innerHTML = department.details(); </script> </body> </html>
Advertisements