There are numerous ways to verify if a certain key exists in an object, but we are going to use the hasOwnProperty() method which I think is simple and easy to use.
In the following example, we have one global object. We simply want to verify if this object has a name property or not. After verification, we will display a Boolean value on the screen. Please have a look over the code example and steps given below.
HTML & CSS
We have 3 elements in the HTML file (div, button, and h1). The div element is just a wrapper for the rest of the elements.
The inner text for the button element is “Check” and for the h1 element is “Result”.
We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.