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.
1 2 3 4 5 6 | var json = { "tutorial" : "nexladder web tutorials" }; if (json.hasOwnProperty( 'tutorial' )) console.log( "Key exist" ); else console.log( "Key not exist" ); //Output Key exist |
1 2 3 4 5 6 | 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.