How to Check If an Object Has a Key in Javascript Using in Operator & HasOwnProperty

You are currently viewing How to Check If an Object Has a Key in Javascript Using in Operator & HasOwnProperty

How to Check if an Object has a Key in JavaScript Using in Operator & hasOwnProperty

<span class="hljs-keyword">if</span> (<span class="hljs-string">'key'</span> <span class="hljs-keyword">in</span> myObj)
 

<span class="hljs-keyword">if</span> (!(<span class="hljs-string">'key'</span> <span class="hljs-keyword">in</span> myObj))
 

myObj.<span class="hljs-title function_">hasOwnProperty</span>(<span class="hljs-string">'key'</span>)
 

myObj.<span class="hljs-title function_">hasOwnProperty</span>(<span class="hljs-string">'myKey'</span>);
 

<span class="hljs-title class_">Object</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">hasOwnProperty</span>.<span class="hljs-title function_">call</span>(myObj, <span class="hljs-string">'myKey'</span>);

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply