JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – does object hasOwnProperty really return boolean

JavaScript Quiz – does object hasOwnProperty really return boolean

Object hasOwnProperty Method

In javascript, object has hasOwnProperty which detect the property exist in object or not. The hasOwnProperty()method returns a boolean value.

hasOwnProperty Method Syntax

obj.hasOwnProperty(prop)

Example of hasOwnProperty Method

const obj = { title: "JavaScript" };

obj.hasOwnProperty("title");
// => true

obj.hasOwnProperty("name");
// => false

For more detail about javascript hasOwnProperty method click here.

JavaScript Question

In this javascript quiz, you need to find out does object hasOwnProperty method really return boolean when key with null value in object.

First, we declare variable detail and assign a blank javascript object to it.

const detail = {};

In the second step, we assign the name key to the detail object and assign a null value.

detail.name = null;

Now, we created another variable name using the const keyword and assign the value return from hasOwnProperty and the “name” string passed to it.

const name = detail.hasOwnProperty("name");

Finally, we are checking the value of name variable using console log.

console.log(name);

JavaScript Quiz Test

const detail = {};
detail.name = null;

const name = detail.hasOwnProperty("name");

console.log(name); // output => ?

Any idea what should the output above code snippet ?

Answer – true

Click To Run Code

Explanation

Now, you know the answer is true but why as we assign value null to name variable does it should be removed. Let explain

Firs, As i already told you that hasOwnProperty return the boolean value which mean either true or false.

And one more thing, assign a null value to object key does not remove key from object.

So, if we check the value of detail then we found name key in it with null value.

const detail = {};
detail.name = null; // { name: null }

At last, That’s why we get an answer “true“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – does set object really store duplicate object
  2. JavaScript Quiz – what does the async array map function return
  3. JavaScript Quiz – can array really be access with an array
  4. JS Quiz – what really is the array index value

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link