{"id":818,"date":"2004-05-05T13:45:47","date_gmt":"2004-05-05T13:45:47","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2004\/05\/checking_variables_exist_in_javascript-shtml\/"},"modified":"2004-05-05T13:45:47","modified_gmt":"2004-05-05T13:45:47","slug":"checking_variables_exist_in_javascript-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/","title":{"rendered":"Checking Variables Exist In JavaScript"},"content":{"rendered":"<p>\nI came across a problem with JavaScript earlier trying to see if a variable actually exists or not. For example&#8230;\n<\/p>\n<div class=\"code\"><code>if (x) {<br \/>\n\/\/ x exists.<br \/>\n} else {<br \/>\n\/\/ x doesn't exist.<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nUnfortunately this causes an error if x hasn&#8217;t been defined, eg&#8230;\n<\/p>\n<div class=\"code\"><code>var x = \"robs test\";<br \/>\n<\/code><\/div>\n<p>\nThe solution came after a read of <a href=\"http:\/\/www.oreilly.com\/catalog\/jscript4\/\">JavaScript: The Definitive Guide<\/a> chapter 4. It explains about the Global Object, and that all global variables are located there. For client side JavaScript this is the Window object. So to see if <code>x<\/code> has been defined we need to check <code>window.x<\/code>, eg&#8230;\n<\/p>\n<div class=\"code\"><code>if (window.x) {<br \/>\n\/\/ x exists.<br \/>\n} else {<br \/>\n\/\/ x doesn't exist.<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nThis means if I forget to define <code>x<\/code> it will default to the block of code saying that x doesn&#8217;t exist. Of course, I shouldn&#8217;t really be in a situation where variables haven&#8217;t been defined, but sanity checking is always a good idea, especially with development code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I came across a problem with JavaScript earlier trying to see if a variable actually exists or not. For example&#8230; if (x) { \/\/ x exists. } else { \/\/ x doesn&#8217;t exist. } Unfortunately this causes an error if x hasn&#8217;t been defined, eg&#8230; var x = &#8220;robs test&#8221;; The solution came after a &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Checking Variables Exist In JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-818","post","type-post","status-publish","format-standard","hentry","category-dev"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Checking Variables Exist In JavaScript - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Checking Variables Exist In JavaScript - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I came across a problem with JavaScript earlier trying to see if a variable actually exists or not. For example&#8230; if (x) { \/\/ x exists. } else { \/\/ x doesn&#039;t exist. } Unfortunately this causes an error if x hasn&#8217;t been defined, eg&#8230; var x = &quot;robs test&quot;; The solution came after a &hellip; Continue reading &quot;Checking Variables Exist In JavaScript&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2004-05-05T13:45:47+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/checking_variables_exist_in_javascript-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/checking_variables_exist_in_javascript-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Checking Variables Exist In JavaScript\",\"datePublished\":\"2004-05-05T13:45:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/checking_variables_exist_in_javascript-shtml\\\/\"},\"wordCount\":133,\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/checking_variables_exist_in_javascript-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/checking_variables_exist_in_javascript-shtml\\\/\",\"name\":\"Checking Variables Exist In JavaScript - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2004-05-05T13:45:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/checking_variables_exist_in_javascript-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/checking_variables_exist_in_javascript-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/checking_variables_exist_in_javascript-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Checking Variables Exist In JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Checking Variables Exist In JavaScript - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Checking Variables Exist In JavaScript - Robert Price","og_description":"I came across a problem with JavaScript earlier trying to see if a variable actually exists or not. For example&#8230; if (x) { \/\/ x exists. } else { \/\/ x doesn't exist. } Unfortunately this causes an error if x hasn&#8217;t been defined, eg&#8230; var x = \"robs test\"; The solution came after a &hellip; Continue reading \"Checking Variables Exist In JavaScript\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/","og_site_name":"Robert Price","article_published_time":"2004-05-05T13:45:47+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Checking Variables Exist In JavaScript","datePublished":"2004-05-05T13:45:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/"},"wordCount":133,"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/","name":"Checking Variables Exist In JavaScript - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2004-05-05T13:45:47+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/checking_variables_exist_in_javascript-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Checking Variables Exist In JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}