Table of contents
1.
Introduction
2.
What is the hasOwnProperty() in Javascript?
3.
Syntax of hasOwnProperty() in Javascript
4.
hasOwnProperty() Parameters
5.
hasOwnProperty() Return Value
6.
Example of Javascript hasOwnProperty()
7.
Supported Browsers
8.
Frequently Asked Questions
8.1.
What does hasOwnProperty do in JavaScript?
8.2.
What is hasOwnProperty like in JavaScript?
8.3.
​​How to check has own property in JavaScript?
8.4.
How to find all objects with property value in JavaScript?
8.5.
Does the hasOwnProperty work on inherited properties or values?
9.
Conclusion
Last Updated: Jul 9, 2024
Easy

JavaScript hasOwnProperty() Method

Introduction

Javascript is one of the significant components of web technology. It is a scripting language. It has various methods and properties that make it easy for developers to develop a website. If you want to revise Javascript concepts, check out this page. In this article, we will discuss one of its essential methods; the javascript hasOwnProperty

JavaScript hasOwnProperty() Method

What is the hasOwnProperty() in Javascript?

The javascript hasOwnProperty() method helps check if the property belongs to the same object or not. The javascript hasOwnProperty() method has a boolean return type. It returns true if the property belongs to the object. If the property does not belong to the object, it returns false. 

Syntax of hasOwnProperty() in Javascript

The hasOwnProperty() method is a built-in method of the JavaScript object. It has the following syntax:

object.hasOwnProperty(property)
You can also try this code with Online Javascript Compiler
Run Code


Here, the object is the name of the object you want to check. Its property is the name of the property you want to check if it is its own property or inherited. The method returns a boolean value, true if the object has its own property, otherwise false.

hasOwnProperty() Parameters

The hasOwnProperty() method in JavaScript takes one parameter which is the property that we are testing the object for. The method is called on an object. It checks if a given property belongs only to the object itself and is not inherited from its prototype chain.

hasOwnProperty() Return Value

The hasOwnProperty() method is used in JavaScript to check if an object has a property that belongs only to itself and is not inherited from its prototype chain. It returns a boolean value of true if the object has the specified property and false if not.

Let us see how we use the javascript hasOwnProperty method through an example:

Example of Javascript hasOwnProperty()

<!DOCTYPE html>
<html>
    <head>
        <title>
            "Demonstration of hasOwnProperty in Javascript."
        </title>
    </head>
    <body>
        <script>
            var coding_ninjas = {  
                name: 'Coding Ninjas Studio',  
                vertical: 'resource',  
            }  
            var intern = {  
                name: 'XYZ',  
            }  
            document.write(coding_ninjas.hasOwnProperty('name'));
            document.write("<br>");
            document.write(coding_ninjas.hasOwnProperty('vertical'));
            document.write("<br>");  
            document.write(intern.hasOwnProperty('name'));
            document.write("<br>");  
            document.write(intern.hasOwnProperty('role'));  
        </script>
    </body>
</html>

Output

output

The output contains four lines. ‘coding_ninjas’ object has ‘name’ and ‘vertical’ properties, so the two lines return true as output. 

'intern' has only one property, 'name', for which the result is true‘role’ is not the property of the ‘intern’ object, so the fourth line of output contains false. 

The javascript hasOwnProperty method does not work on Inherited Properties. Inherited properties are the ones that the object inherits from the prototype object. The method returns true only on non-inherited properties.

Supported Browsers

Below is the browser lists that support the hasOwnProperty() method in Javascript.

  • Chrome (1 and above)
  • Edge (12 and above)
  • Opera (5 and above)
  • Safari (3 and above)
  • Firefox (1 and above)

Frequently Asked Questions

What does hasOwnProperty do in JavaScript?

In JavaScript, hasOwnProperty checks if an object directly possesses a specific property, ignoring inherited properties. It returns true if the property exists in the object, false if not.

What is hasOwnProperty like in JavaScript?

In JavaScript, hasOwnProperty is like a detective tool. It checks if an object actually has a specific property, excluding properties inherited from its parent objects. It returns true or false.

​​How to check has own property in JavaScript?

In JavaScript, you can check if an object has its own property using the hasOwnProperty() method. This method returns a boolean value indicating whether the object has the specified property as a direct property of the object itself, rather than inherited from its prototype chain.

How to find all objects with property value in JavaScript?

To find all objects with a specific property value in JavaScript, you would typically iterate through an array of objects, checking each object's property value and collecting matching objects into a new array or performing the desired action with them.

Does the hasOwnProperty work on inherited properties or values?

No, the hasOwnProperty does not work on inherited properties or values.

Conclusion

In this article, we learned the javascript hasOwnProperty(). The hasOwnProperty() method in JavaScript provides a convenient way to determine if an object has a specific property as its own property, rather than inherited from its prototype chain

Wanna learn more about web technologies? Why not have a look at web technologies on Code360? Don’t stop yourself here. Practice data structures and algorithmsinterview questionsDBMScomputer networks, and operating systems to crack the interviews of big tech giants. Explore other fields like machine learningdeep learningcomputer vision, and big data. Also, check out Interview Experiences for different companies.

Happy learning!

Live masterclass