How to use array that include and check an object against a property of an object ? Last Updated : 17 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Array.includes() Method: In JavaScript, includes() method is used to determine that a particular element is present in an array or not. It returns true if the element is present and false when it is absent. Syntax: array_name.includes(searchElement, ?fromIndex) Parameters: searchElement: The element to be search in the array.fromIndex: The index from which the element is to be searched. This is an optional parameter. Example: HTML <!DOCTYPE html> <html lang="en"> <body> <h2> Checking if the countries array contains Japan ---> <span id="ans"></span> </h2> <h2> Checking for Japan in the countries array from index 2 ---> <span id="ans2"></span> </h2> <script> let countries = ["India", "Japan", "Canada", "Germany", "Australia"]; // 1st Output let ans = document.querySelector("#ans"); let output = countries.includes("Japan"); ans.append(output); // 2nd Output let ans2 = document.querySelector("#ans2"); let output2 = countries.includes("Japan", 2); ans2.append(output2); </script> </body> </html> Output: Checking for a property in an Object: To check that an Object contains a particular property or not, we have 3 methods to do this- 1. Using in operator: It returns true if the property exists in the object and false if it doesn't exists. It checks for both the own and inherited properties of the object. Syntax: 'property_name' in object_name Example: JavaScript <script> let Person = { name: "durgesh", age: 16 } // Output: true console.log('name' in Person) // Returns true for an inherited // property // Output: true console.log('toString' in Person) // Output: false console.log('gender' in Person) </script> Output: true true false Note: The toString() method used in above example as an inherited property from prototype object. The 'in' operator returns true for prototype inherited properties. Using hasOwnProperty() Method: It returns true if the property exists in the object and false if it doesn't exists. It checks only for 'own' properties(The properties that are defined inside the object) of the object. Syntax: object_name.hasOwnProperty('property_name') Example: JavaScript <script> let Person = { name: 'Durgesh', age: 16 }; // Output: true console.log(Person.hasOwnProperty('name')) /* hasOwnProperty() doesn't checks for inherited properties of the object. */ /* toString() is an inherited property. */ // Output: false console.log(Person.hasOwnProperty('toString')); // Output: false console.log(Person.hasOwnProperty('gender')); </script> Output: true false false Comparing with undefined: Evaluating a property that doesn't exists in an object results in undefined. So we can compare the result with undefined to know that a property is present or absent. Example: JavaScript let Person = { name: 'Durgesh', age: 16 }; // Returns true if the property is present // Output: true console.log(Person.name !== undefined) // Returns true for inherited property // Output: true console.log(Person.toString !== undefined) // Output: false console.log(Person.gender !== undefined) Output: true true false Note: This is an unpleasant approach compared to the above two because, if a property is defined as undefined in the object then this method evaluates it to false. It is advisable to use above two methods if there is a possibility that your object's property value can be undefined. JavaScript let Person = { // Setting name to undefined name: undefined, age: 16 }; /* This evaluates to false despite the fact that name property exists */ // Output: false console.log(Person.name!==undefined) Output: false Comment More infoAdvertise with us Next Article How to use array that include and check an object against a property of an object ? D durgeshpushpakar Follow Improve Article Tags : JavaScript Web Technologies javascript-object JavaScript-Properties JavaScript-Questions +1 More Similar Reads How to Check Presence of a Specific Object Property in an Array ? Checking for the presence of a specific object property in an array involves determining whether any object within the array possesses the specified property. This task is commonly encountered when working with arrays of objects and needing to verify if a particular property exists across any of the 3 min read How to Check if an Array Includes an Object in TypeScript ? In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method. There are several ways to check if an array includes an object 3 min read How to throw an error when using a property of an object ? In this article, we will try to understand how we may throw an error when using a property of an object with the help of certain theoretical explanations as well as coding examples in JavaScript. Let us first have a look over the below section which shows the syntax for creating an object having cer 4 min read How to check if an array includes an object in JavaScript ? Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array.How to check if an array inclu 3 min read How to check whether an array includes a particular value or not in JavaScript ? In this article, we will discuss the construction of an array followed by checking whether any particular value which is required by the user is included (or present) in the array or not. But let us first see how we could create an array in JavaScript using the following syntax- Syntax: The followin 3 min read How to Remove a Property from All Objects in an Array in JavaScript? To remove a property from all objects in an array in JavaScript, you can use the forEach() method to iterate over each object in the array and use the delete operator to remove the specified property.Example:JavaScriptconst arrayOfObjects = [ { name: "Alice", age: 25, city: "New York" }, { name: "Bo 2 min read How to Search Character in List of Array Object Inside an Array Object in JavaScript ? Searching for a character in a list of array objects inside an array object involves inspecting each array object, and then examining each array within for the presence of the specified character. The goal is to determine if the character exists within any of the nested arrays. There are several way 4 min read How to Find Property Values in an Array of Object using if/else Condition in JavaScript ? Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects. Table of ContentUsing Array Find MethodUsing Array Filter MethodUsing For Of LoopUsing Array Map MethodUsing Array Reduce MethodUsing Array Some MethodUsing Array Find 5 min read How to Check if an Object has a Specific Property in JavaScript ? In JavaScript, objects can have properties that store data or functions. Sometimes it is necessary to check whether an object has a specific property. This can be useful, for example, when you want to avoid calling a function on an object that doesn't have that function defined as a property. In thi 3 min read How to Detect an Undefined Object Property in JavaScript ? Detecting an undefined object property is the process of determining whether an object contains a certain property, and if it does, whether the value of that property is undefined. This is an important concept in JavaScript programming, as it helps to prevent errors that can occur when attempting to 3 min read Like