Is there a way to print all methods of an object in JavaScript?

In JavaScript, methods are object properties that contain functions. Sometimes you need to inspect an object to discover all its available methods. This tutorial shows how to print all methods of an object using different approaches.

A method is simply a function stored as an object property. When you call obj.methodName(), you're executing the function stored in that property. JavaScript provides several ways to discover these function properties.

Using Object.getOwnPropertyNames() Method

The Object.getOwnPropertyNames() method returns an array of all properties (including non-enumerable ones) found directly on an object. We can filter this array to find only function properties.

Syntax

Object.getOwnPropertyNames(obj).filter(function (p) {
   return typeof obj[p] === 'function';
});

Example 1: Custom Object Methods

<html>
<body>
   <h3>Print all methods of an object using <i>Object.getOwnPropertyNames()</i></h3>
</body>
<script>
   var obj = {
      name: 'John',
      age: 32,
      myMethod1: function(age) { return age; },
      myMethod2: function(name) { return name; }
   };
   
   var methods = Object.getOwnPropertyNames(obj).filter(function(p) {
      return typeof obj[p] === 'function';
   });
   
   document.write("obj methods: " + methods.join(', '));
</script>
</html>
obj methods: myMethod1, myMethod2

Example 2: Built-in Object Methods

<html>
<body>
   <h3>Math and Array Methods</h3>
</body>
<script>
   var mathMethods = Object.getOwnPropertyNames(Math).filter(function(p) {
      return typeof Math[p] === 'function';
   });
   
   var arrayMethods = Object.getOwnPropertyNames(Array).filter(function(q) {
      return typeof Array[q] === 'function';
   });
   
   document.write("Math methods: " + mathMethods.slice(0, 5).join(', ') + "...");
   document.write("<br>Array methods: " + arrayMethods.slice(0, 5).join(', ') + "...");
</script>
</html>
Math methods: abs, acos, acosh, asin, asinh...
Array methods: isArray, from, of, fromAsync, isTemplateObject...

Using Object.keys() with typeof Operator

The Object.keys() method returns only enumerable properties, which is often what you want. Combined with typeof, you can filter for function properties.

Syntax

function getAllMethods(obj) {
   return Object.keys(obj)
      .filter((key) => typeof obj[key] === 'function')
      .map((key) => obj[key]);
}

Example

<html>
<body>
   <h3>Print all methods using Object.keys() and typeof</h3>
   <button onclick="execute()">Click Here</button>
   <p id="result"></p>
</body>
<script>
   function TestObject() {
      this.method1 = function() { return "Method 1"; };
      this.method2 = function() { return "Method 2"; };
      this.property = "Not a method";
   }
   
   function getAllMethods(obj) {
      return Object.keys(obj)
         .filter((key) => typeof obj[key] === 'function')
         .map((key) => key);
   }
   
   function execute() {
      var testObj = new TestObject();
      var methods = getAllMethods(testObj);
      document.getElementById("result").innerHTML = "Methods found: " + methods.join(', ');
   }
</script>
</html>
Methods found: method1, method2

Comparison of Methods

Method Includes Non-enumerable Best For
Object.getOwnPropertyNames() Yes Built-in objects, complete inspection
Object.keys() No User-defined objects, standard properties

Conclusion

Use Object.getOwnPropertyNames() for comprehensive method discovery including built-in objects, or Object.keys() for simpler cases with user-defined objects. Both approaches filter properties by checking if their type is "function".

Updated on: 2026-03-15T23:18:59+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements