Existence check and enumeration of methods and properties

When dynamically defining new properties and methods in an object, it can be important to check whether such methods and properties already exist before using them. The in operator can be used in javascript for this. It has the following syntax:

"property|method" in object

in quotes is the name of the property or method, and after in – the name of the object. If there is a property or method with the same name, then the operator returns true. If not, it returns false.

For example, let’s find out if an object has a number of properties:

const user = {};
user.name = "Tom";
user.age = 26;
user.print = function(){
    
    console.log(this.name);
    console.log(this.age);
};
const hasNameProp = "name" in user;
console.log(hasNameProp); // true - the name property exists in user

const hasWeightProp = "weight" in user;
console.log(hasWeightProp); // false - user does not have a property or method called weight

Using the expression “name” in user, we check if the user object has the “name” property and pass the result of the check to the hasNameProp constant. Next, in a similar way, we check the presence of the property weight.

In a similar way, you can check the presence of methods:

 
const hasPrintMethod = "print" in user;
console.log(hasPrintMethod); // true - user has a print method

An alternative way is to use the undefined value. If a property or method is undefined, then that property or method is not defined:

const hasNameProp = user.name!==undefined;
console.log(hasNameProp); // true
const hasWeightProp = user.weight!==undefined;
console.log(hasWeightProp); // false

And since objects represent the Object type, and therefore have all of its methods and properties, objects can also use the hasOwnProperty() method, which is defined in the Object type:

const hasNameProp = user.hasOwnProperty("name");
console.log(hasNameProp); // true
const hasPrintMethod = user.hasOwnProperty("print");
console.log(hasPrintMethod); // true
const hasWeightProp = user.hasOwnProperty("weight");
console.log(hasWeightProp); // false

Iterating over properties and methods

With a for loop, we can iterate over an object like a regular array and get all of its properties and methods and their values:

const tom = {
    name: "Tom",
    age: 26,
    print(){
        console.log(`Name: ${this.name}  Age: ${this.age}`);
    }
};

for(const prop in tom) {
    console.log(prop, " : ", tom[prop]);
}
And when run, the browser console will display the following output:

name : Tom
age: 26
print : print(){
	console.log(`Name: ${this.name} Age: ${this.age}`);
}

Functions Object.entries, Object.keys, Object, values

Using the additional functions Object.entries, Object.keys, and Object,values, you can get all the properties (including methods) of an object and their values.

Object.entries()

The Object.entries() function takes an object as a parameter and returns an array of property_name-value pairs, where each property-value pair represents an array. For example:

const tom = {
    name: "Tom",
    age: 26,
    print(){
        console.log(`Name: ${this.name}  Age: ${this.age}`);
    }
};
 
for(const prop of Object.entries(tom)) {
    console.log(prop);
}
[js]

<h3>Console output:</h3>

[js]
["name", "Tom"]
["age", 26]
["print", ƒ]

Object.keys()

The Object.keys() function allows you to get an array of the names of all the properties of an object. For example, take the above-defined object tom:

const tom = {
    name: "Tom",
    age: 26,
    print(){
        console.log(`Name: ${this.name}  Age: ${this.age}`);
    }
};
 console.log(Object.keys(tom)); // ["name", "age", "print"]
 

Accordingly, you can iterate over this set and get the property values:

for(const prop of Object.keys(tom)) {
    const value = tom[prop]; // get property value by name
    console.log(prop, value);
}

Console output:

name Tom
age 26
print ƒ print(){
        console.log(`Name: ${this.name} Age: ${this.age}`);
    }
    

Object.values()

The Object.values() function returns an array that contains all the object’s property values:

const volume = {
    name: "Tom",
    age: 26
    print(){
        console.log(`Name: ${this.name} Age: ${this.age}`);
    }
};
 console.log(Object.values(tom)); // ["Tom", 26, print()]