{"id":1076,"date":"2024-07-17T19:37:28","date_gmt":"2024-07-17T19:37:28","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=1076"},"modified":"2025-05-15T05:00:07","modified_gmt":"2025-05-15T05:00:07","slug":"how-to-check-if-a-key-exists-in-a-javascript-object","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/how-to-check-if-a-key-exists-in-a-javascript-object\/","title":{"rendered":"How to Check If a Key Exists in a JavaScript Object"},"content":{"rendered":"\n<p>Hello Nerds<\/p>\n\n\n\n<p>Checking if a <strong>specific key exists in a JavaScript Object<\/strong> is a common task in programming.  We will here describe some most common patterns with which you can get check if key exists in a Javascript Object<\/p>\n\n\n\n<p>In the first way, you can use very simple way<code> in <\/code>operator to check if a particular key or property exists in your javascript object. The operator returns <code>true <\/code>if the specified key existing in the object, otherwise it will return <code>false<\/code><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">\/\/ This is a Sample object\nvar myCar = {\n    make: &quot;Tata&quot;,\n    model: &quot;Punch&quot;,\n    year: 2024\n};\n\n\/\/ This line Test if a key exists in the object\nif(&quot;model&quot; in myCar === true) {\n    alert(&quot;Key Exists in the object&quot;);\n} else {\n    alert(&quot;Key doesn't exist in the object&quot;);\n}<\/pre><\/div>\n\n\n\n<p>Lets deep dive a little bit more, here if you set the property of an object to <code>undefined<\/code> and if you do not delete it, then the in operator will return <code>true<\/code> for that specific property. lets see this with an example. <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">\/\/ This is a Sample object\nvar myCar = {\n    make: &quot;Tata&quot;,\n    model: &quot;Fortuner&quot;,\n    year: 2024\n};\n\n\/\/ Setting a property to undefined\nmyCar.model = undefined;\n\n\/\/ Deleting a property\ndelete myCar.year;\n\n\/\/ Test if properties exist\nconsole.log(&quot;make&quot; in myCar);  \/\/ Prints: true\nconsole.log(&quot;model&quot; in myCar); \/\/ Prints: true\nconsole.log(&quot;year&quot; in myCar);  \/\/ Prints: false<\/pre><\/div>\n\n\n\n<p>We have seen <code>in<\/code> operator here to check if key exists in our code or not with <code>true<\/code> and <code>false<\/code> statement<\/p>\n\n\n\n<p>There are more ways to do this let have some examples:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Using the <code>in<\/code> Operator<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">const obj = { \n    make: &quot;Tata&quot;,\n    model: &quot;Fortuner&quot;,\n    year: 2024\n};\nconsole.log('model' in obj); \/\/ true<\/pre><\/div>\n\n\n\n<p>2. <strong>Using <code>hasOwnProperty()<\/code> Method<\/strong><\/p>\n\n\n\n<p>The <code>hasOwnProperty()<\/code> method checks if the key exists directly on the object, ignoring inherited properties.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">const obj = {    \n  make: &quot;Tata&quot;,\n  model: &quot;Fortuner&quot;,\n  year: 2024 \n  };\nconsole.log(obj.hasOwnProperty('model')); \/\/ true\n<\/pre><\/div>\n\n\n\n<p>3. <strong>Using <code>undefined<\/code> Check<\/strong><\/p>\n\n\n\n<p>You can check if the value of the key is <code>undefined<\/code>, but this method can be less reliable if the key exists and its value is actually <code>undefined<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">const obj = { \n  make: &quot;Tata&quot;,\n  model: &quot;Fortuner&quot;,\n  year: 2024 \n};\nconsole.log(obj.model !== undefined); \/\/ true\n<\/pre><\/div>\n\n\n\n<p><strong>4. Using <code>Object.keys()<\/code><\/strong><\/p>\n\n\n\n<p>The <code>Object.keys()<\/code> method returns an array of the object\u2019s own property names. You can check if the array includes the specified key.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">const obj = { \n  make: &quot;Tata&quot;,\n  model: &quot;Fortuner&quot;,\n  year: 2024 \n};\nconsole.log(Object.keys(obj).includes('model')); \/\/ true\n<\/pre><\/div>\n\n\n\n<p>Depending on your specific needs, you can use any of these methods to check for the existence of a key in a JavaScript object. The <code>in<\/code> operator and <code>hasOwnProperty()<\/code> method are generally the most straightforward and reliable choices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Also Read these blogs<\/h2>\n\n\n\n<p class=\"has-black-color has-text-color has-background has-link-color wp-elements-4f76e5f04bd570a3aa439042193b0c79\" style=\"background-color:#f0f0f0\"><a href=\"https:\/\/www.appfinz.com\/blogs\/how-to-use-jquery-animation-effects\/\"><strong>How to use jQuery Animation Effects<\/strong><\/a><\/p>\n\n\n\n<p class=\"has-black-color has-text-color has-background has-link-color wp-elements-54dfd959c0b4a9fa44b530f07cbd9c0d\" style=\"background-color:#f0f0f0\"><a href=\"https:\/\/www.appfinz.com\/blogs\/how-to-check-if-a-key-exists-in-a-javascript-object\/\"><strong>Check If a Key Exists in a JavaScript<\/strong><\/a><\/p>\n\n\n\n<p class=\"has-black-color has-text-color has-background has-link-color wp-elements-c3ef971df0610c2f3c4bc82dfa75b3f2\" style=\"background-color:#f0f0f0\"><a href=\"https:\/\/www.appfinz.com\/blogs\/php-common-how-to-solutions\/\"><strong>PHP Common How to Solutions<\/strong><\/a><\/p>\n\n\n\n<p class=\"has-black-color has-text-color has-background has-link-color wp-elements-b0480d8bd15322f512da968eb512bd84\" style=\"background-color:#f0f0f0\"><a href=\"https:\/\/www.appfinz.com\/blogs\/understanding-this-in-javascript\/\"><strong>Understanding \u201cthis\u201d in JavaScript<\/strong><\/a><\/p>\n\n\n\n<p class=\"has-black-color has-text-color has-background has-link-color wp-elements-39193b4dfcabb07cc7676422292a9fa1\" style=\"background-color:#f0f0f0\"><strong><a href=\"https:\/\/www.appfinz.com\/blogs\/paypal-payment-gateway-integration-in-laravel-11\/\">Paypal Payment Gateway Integration in Laravel 11<\/a><\/strong><\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e6eef4\">If you\u2019re planning to revamp your online presence, don\u2019t miss exploring our\u00a0<a href=\"https:\/\/www.appfinz.com\/website-designing-company-in-delhi\"><strong>website designing services<\/strong><\/a>\u00a0and custom\u00a0<strong><a href=\"https:\/\/www.appfinz.com\/website-development-company-in-delhi\">website development<\/a><\/strong>\u00a0solutions tailored to your business needs.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Nerds Checking if a specific key exists in a JavaScript Object is a common<\/p>\n","protected":false},"author":1,"featured_media":1092,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[],"class_list":["post-1076","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=1076"}],"version-history":[{"count":6,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1076\/revisions"}],"predecessor-version":[{"id":1364,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1076\/revisions\/1364"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/1092"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=1076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=1076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=1076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}