JavaScript’s hasOwnProperty method is used to check if a given object has a specific property.
The property checked by the hasOwnProperty method is internal to the object in question, that is, it must have been defined by the user.
If the property passed to hasOwnProperty exists on the object, the method returns true, otherwise it returns false .
Example 1:
In this example, we create a person object and add a name property, then we use the hasOwnProperty to verify that this property is part of our object.
1var person = {
2 "name": "Brian",
3 "lastName": "Scott"
4};
5
6console.log( person.hasOwnProperty("name") );
Output:
1true
Example 2:
In this example, we are going to declare an object named car and assign it a property called doors, so we can use hasOwnProperty to check this property.
1var car = {
2 "doors": 4,
3 "engine": 2.0,
4 "color": "black"
5};
6
7var hasDoor = car.hasOwnProperty("doors");
8
9console.log(hasDoor);
Output:
1true
Example 3:
Here we will purposely misspell the property name of our object so that the hasOwnProperty method returns false .
1var shoppingCart = {
2 "fruits": true,
3 "bread": false
4};
5
6var hasFruits = shoppingCart.hasOwnProperty("fruites");
7
8console.log(hasFruits);
Output:
1false
Why is it important to use the hasOwnProperty method?
Let’s look at the importance of using the hasOwnProperty method to check if a property exists within an object in JavaScript.
Everything in JavaScript is an object.
That’s right, any element created in JavaScript is considered an object. Numbers, Text Strings, Arrays and any other JavaScript data type inherit properties from a parent object.
Example 1:
The numeric data type in JavaScript is considered an object.
1var number = 10.22333;
2var otherNumber = number.toPrecision(3);
3
4console.log(otherNumber);
Output:
110.2
Example 2:
The string data type is also considered an object.
1var name = "Peter Max";
2var char = name.charAt(2);
3
4console.log(char);
Output:
1t
Example 3:
Even arrays in JavaScript are objects.
1var cars = ["BMW", "Audi", "Aston Martin"];
2
3var carIndexNumber = cars.indexOf("BMW");
4
5console.log(carIndexNumber);
Output:
10
In the previous examples, all objects inherit properties from the main object, Object.
The “in” command in JavaScript
New JavaScript programmers typically use the in command to check if a property exists on a given object.
Example:
1var car = {
2 "doors": 2,
3 "engine": 1.2,
4 "color": "yellow"
5};
6
7if ("engine" in car) {
8 console.log("true");
9} else {
10 console.log("false");
11}
Output:
1true
The problem is that the in command is comprehensive. It will do a search for all properties of the object. User-created properties and properties inherited from the parent object are included in the check.
This can cause a logic error if the programmer happens to run a piece of code only if an object property exists.
But the programmer wants to check a default property of the object? Or a user-created property?
Example:
1var car = {
2 "doors": 2,
3 "engine": 1.2,
4 "color": "yellow"
5};
6
7if ("toString" in car) {
8 console.log("true");
9} else {
10 console.log("false");
11}
Output:
1true
In the previous example, the command in returns true, that is, it assumes that the toString property exists in our object, even though this property has not been declared by us.
The toString property/method is inherited from the parent object. To avoid this confusion always use the hasOwnProperty method .
Example:
1var car = {
2 "doors": 2,
3 "engine": 1.2,
4 "color": "yellow"
5};
6
7if ( car.hasOwnProperty("toString") ) {
8 console.log("true");
9} else {
10 console.log("false");
11}
Output:
1false
Now the return value was false. This is because, as we saw earlier, the hasOwnProperty method only searches for user-defined properties.