JavaScript | Object Methods
Object Methods in JavaScript can be accessed by using functions. Functions in JavaScript are stored as property values. The objects can also be called without using bracket ().
- In a method, ‘this’ refers to the owner object.
- Additional information can also be added along with the object method.
Syntax:
objectName.methodName()
Properties: A function may be divided into different property values, which are then combined and returned together.
For Ex: Student function contains the properties:
- name
- class
- section
Return Value: It returns methods/functions stored as object properties.
Example 1: This example use function definition as property value.
<!DOCTYPE html> <html> <head> <title> JavaScript Object Methods </title> </head> <body> <h1>Geeks</h1> <h3>JavaScript Object Method</h3> <p> studentDetail is a function definition, it is stored as a property value. </p> <p id="gfg"></p> <script> // Object creation var student = { name: "Martin", class : "12th", section : "A", studentDetails : function() { return this.name + " " + this.class + " " + this.section + " "; } }; // Display object data document.getElementById("gfg").innerHTML = student.studentDetails(); </script> </body> </html> |
Output:

Example 2: This example use storing property values and accessing without bracket ().
<!DOCTYPE html> <html> <head> <title> JavaScript Object Methods </title> </head> <body> <h1>Geeks</h1> <h3>JavaScript Object Method</h3> <p> studentDetail is a function definition, it is stored as a property value. </p> <p> Function definition is returned if we don't use (). </p> <p id="gfg"></p> <script> // Object creation var student = { name: "Martin", class : "12th", section : "A", studentDetails : function() { return this.name + " " + this.class + " " + this.section + " "; } }; // Display object data document.getElementById("gfg").innerHTML = student.studentDetails; </script> </body> </html> |
Output:

Example 3: Using function definition as property value and accessing with additional details.
<!DOCTYPE html> <html> <head> <title> JavaScript Object Methods </title> </head> <body> <h1>Geeks</h1> <h3>JavaScript Object Method</h3> <p> studentDetail is a function definition, it is stored as a property value. </p> <p id="gfg"></p> <script> // Object creation var student = { name: "Martin", class : "12th", section : "A", studentDetails : function() { return this.name + " " + this.class + " " + this.section + " "; } }; // Display object data document.getElementById("gfg").innerHTML = "STUDENT " + student.studentDetails(); </script> </body> </html> |
Output:

Recommended Posts:
- How to access an object having spaces in the object's key using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- JavaScript | Set Date Methods
- JavaScript | Get Date Methods
- JavaScript Basic Array Methods
- JavaScript | Array Iteration Methods
- JavaScript methods to increment/decrement alphabetical letters
- Map vs Object in JavaScript
- How to get the first key name of a JavaScript object ?
- Object.is( ) in JavaScript
- How to get a key in a JavaScript object by its value ?
- Object.keys( ) In JavaScript
- JavaScript | Object Accessors
- Object.isFrozen( ) In JavaScript
- Object.isSealed( ) In JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



