Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Object getOwnPropertyNames() Method

JavaScript Object getOwnPropertyNames() Method

The Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties.

Syntax: 

Object.getOwnPropertyNames(obj)

Parameters: This method accepts a single parameter as mentioned above and described below: 

  • obj: This parameter holds the object whose enumerable and non-enumerable properties are to be returned.

Return value: This method returns an array of strings that corresponds to the properties found directly in the given object.

Below examples illustrate the Object.getOwnPropertyNames() method in JavaScript:

Example 1: In this example, we will check if an object contains some properties or not using the Object.getOwnPropertyNames() method in JavaScript.

javascript




const gfg = {
    val1: "Geek1",
    val2: "Geek2",
    val3: "Geek3",
    val4: "Geek4"
};
console.log(Object.getOwnPropertyNames(gfg));
  
let gfg2 = { val1: 'Geek1', val2: 'Geek2', val3: 'Geek3' };
console.log(Object.getOwnPropertyNames(gfg2).sort());
  
Object.getOwnPropertyNames(gfg2).
    forEach(function (val, idx, array) {
        console.log(val + ': ' + gfg2[val]);
  
    });


Output: 

Array ["val1", "val2", "val3", "val4"]
Array ["val1", "val2", "val3"]
"val1 : Geek1"
"val2 : Geek2"
"val3 : Geek3"

Example 2: In this example, we will check if an object contains some properties or not using the Object.getOwnPropertyNames() method in JavaScript.

javascript




function ParentClass() { }
ParentClass.prototype.inheritedMethod = function () { };
  
function ChildClass() {
    this.prop = 5;
    this.method = function () { };
}
ChildClass.prototype = new ParentClass;
ChildClass.prototype.prototypeMethod = function () { };
  
console.log(
    Object.getOwnPropertyNames(
        new ChildClass()
    )
);
  
let my_obj = Object.create({}, {
    getFoo: {
        value: function () { return this.foo; },
        enumerable: false
    }
});
my_obj.foo = 1;
  
console.log(Object.getOwnPropertyNames(my_obj).sort());


Output: 

Array ["prop", "method"]
Array ["foo", "getFoo"]

We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.

Supported Browsers: The browsers supported by Object.getOwnPropertyNames() method are listed below:

  • Google Chrome 5 and above
  • Edge 12 and above
  • Firefox 4 and above
  • Internet Explorer 9 and above
  • Opera 12 and above
  • Safari 5 and above
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments