In this article, We’ll see how to check a key exist or not in json?
before see the example, we’ll read about hasOwnProperty
method and why we use hasOwnProperty
method.
hasOwnProperty()
method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
To check the key exist or not in json, we’ll use hasOwnProperty()
method.
var json = {"tutorial": "nexladder web tutorials"}; if (json.hasOwnProperty('tutorial')) console.log("Key exist"); else console.log("Key not exist"); //Output Key exist
var json = {"tutorial": "nexladder web tutorials"}; if (json.hasOwnProperty('webtutorial')) console.log("Key exist"); else console.log("Key not exist"); //Output Key not exist
That’s it!. Please share your thoughts or suggestions in the comments below.