{"id":1218,"date":"2022-08-30T15:14:21","date_gmt":"2022-08-30T15:14:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/jquery-ajax-and-json-how-to-check-if-undefined-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:21","modified_gmt":"2022-08-30T15:14:21","slug":"jquery-ajax-and-json-how-to-check-if-undefined-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/jquery-ajax-and-json-how-to-check-if-undefined-collection-of-common-programming-errors\/","title":{"rendered":"Jquery Ajax and Json : How to check if undefined-Collection of common programming errors"},"content":{"rendered":"<p>I got this Jquery code :<\/p>\n<pre><code>$.ajax({\n    url: \"?module=gestionApplication&amp;action=getTests&amp;scenario=\"+encodeURI(scenario)+\"&amp;application=\"+$application,\n    dataType:'json',\n    success: function( data ) {\n        $.each(data, function(i, item) {\n            $(\"#tests tbody\").append($tr+\"<\/code><\/pre>\n<td title='\"+item.DESCRIPTION+\"'><code>\"+item.ID+\"<\/code><\/td>\n<p><code>\" + \"<\/code><\/p>\n<td><code>\"+ \"Header : \"+item.HEADER + '<br \/>\n' + \"M\u00e9thode : \"+item.METHODE + '<br \/>\n' + \"PostBody : \"+item.POSTBODY + '<br \/>\n' + \"URL : \"+item.URL + '<br \/>\n' + \"ParseReponse : \"+item.PARSEREPONSE + '<br \/>\n' + \"<\/code><\/td>\n<p><code>\" +<\/code><\/p>\n<p>So i got a JSON response from my server, but not all fields are full. Sometimes item.HEADER or item.METHODE can not be defined, so I get &#8220;undefined&#8221; text in my table. Problem is, I&#8217;m French and I would like different text and not this &#8216;undefined&#8217;.<\/p>\n<p>So how can I test if the variable is defined or not? Or even better, is it possible to change this &#8216;undefined&#8217; text to different text in case the variable is not defined?<\/p>\n<ol>\n<li>\n<p>You can do a quick conditional \/ logical-OR check within your concat:<\/p>\n<pre><code>\"Header : \" + (item.HEADER || '') + '<br \/>' +\n<\/code><\/pre>\n<p>so now, if <code>item.HEADER<\/code> is <code>undefined<\/code> we will encounter the empty string instead. Of course you could also use a more expressive string like <code>\"empty\"<\/code> or whatnot.<\/p>\n<\/li>\n<li>\n<pre><code>if (typeof variable == \"undefined\")\n{\n    \/\/ variable is undefined\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Use a ternary operator&#8230; ( test ? do if true : do if false )<\/p>\n<pre><code>...+( item.HEADER ? item.HEADER : \"something french\" )+...\n<\/code><\/pre>\n<\/li>\n<li>\n<p>easily done like so:<\/p>\n<pre><code>if (item.HEADER === undefined) {\n    item.HEADER = 'ind\u00e9fini';\n}\n\n\/\/ or \n\nif (typeof item.HEADER === 'undefined') {\n    item.HEADER = 'ind\u00e9fini';\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:10:33. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I got this Jquery code : $.ajax({ url: &#8220;?module=gestionApplication&amp;action=getTests&amp;scenario=&#8221;+encodeURI(scenario)+&#8221;&amp;application=&#8221;+$application, dataType:&#8217;json&#8217;, success: function( data ) { $.each(data, function(i, item) { $(&#8220;#tests tbody&#8221;).append($tr+&#8221; &#8220;+item.ID+&#8221; &#8221; + &#8221; &#8220;+ &#8220;Header : &#8220;+item.HEADER + &#8216; &#8216; + &#8220;M\u00e9thode : &#8220;+item.METHODE + &#8216; &#8216; + &#8220;PostBody : &#8220;+item.POSTBODY + &#8216; &#8216; + &#8220;URL : &#8220;+item.URL + &#8216; &#8216; + &#8220;ParseReponse [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1218","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1218"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1218\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}