JavaScript | Object Methods - GeeksforGeeks

04 Aug, 2021
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.  
HTML
<!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 ().  
HTML
<!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.  
HTML
<!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:  
Supported Browser:
Google Chrome
Microsoft Edge
Firefox
safari 
ysachin2314
javascript-object
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
Set the value of an input field in JavaScript
Differences between Functional Components and Class Components in React
Form validation using HTML and JavaScript
Difference between var, let and const keywords in JavaScript
Express.js express.Router() Function
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to set the default value for an HTML <select> element ?
Top 10 Angular Libraries For Web Developers