Tuesday, 30 January 2018

javascript - Is there any way to distinguish between an unset property and a property set to undefined?





Say I have the object testObject = {a: undefined}. If I then console.log(testObject.a), I get undefined. But the same happens if I console.log(testObject.b), which doesn't exist. Is there any way in JavaScript to distinguish between a and b here? I ask mostly out of curiosity, I have no use case.


Answer



hasOwnProperty() method returns a boolean indicating whether the object has the specified property as own (not inherited) property.



In given case -




  testObject = {a: undefined};

testObject.hasOwnProperty('a') //true
testObject.hasOwnProperty('b') //false

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...