{"id":475,"date":"2019-02-12T04:37:56","date_gmt":"2019-02-12T04:37:56","guid":{"rendered":"http:\/\/www.projectimmerse.com\/blog\/?p=475"},"modified":"2020-06-17T07:05:55","modified_gmt":"2020-06-17T07:05:55","slug":"fixing-a-network-404-error-using-typeof","status":"publish","type":"post","link":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/","title":{"rendered":"Fixing a network 404 error using typeof"},"content":{"rendered":"<p>Javascript&#8217;s <a href=\"https:\/\/www.w3schools.com\/js\/js_datatypes.asp\" title=\"typeof\">typeof<\/a> operator is an essential debugging tool that many young programmers often miss. Countless times I&#8217;ve run into situations where my program wasn&#8217;t working because either my variable was unnoticeably coerced or I was just completely unaware of a data type. As mentioned in a previous post &#8211; a season programmer should know their data types! Another thing a seasoned programmer ought to know is to prevent themselves from being too arrogant &#8211; yes, we need help and using the &#8220;typeof&#8221; operator is there to the rescue. <\/p>\n<p><!--more--><\/p>\n<p>I&#8217;ve been getting a lot of optimization requests from my consulting clients &#8211; and, optimization is such a broad term. It could mean anything, but generally speaking we are talking about site speed. If we narrow that down, well we get a really long list of solutions thanks to Google Page Insights. Even more, Page Insights is powered by <a href=\"https:\/\/developers.google.com\/web\/tools\/lighthouse\/\" title=\"Lighthouse\">Lighthouse<\/a> and I&#8217;ve done my research &#8211; Lighthouse Technology is totally legit. <\/p>\n<p>Back to the typeof operator &#8211; this solution didn&#8217;t arrive so quickly when I was trying to debug a situation where some variable was &#8220;undefined&#8221;. Yeah, undefined &#8211; such a mystical data type. It&#8217;s almost up there with &#8220;null&#8221; and &#8220;NaN&#8221;. Javascript can be so weird sometimes, you don&#8217;t really know what you&#8217;re dealing with. <\/p>\n<p>And so some variable was floating around, let&#8217;s call this variable &#8220;time&#8221;. So in a program it could look sometime like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\nif (time) {\r\n\r\n \/\/ ... a bunch of code ...\r\n \/\/ ... a bunch of code ...\r\n\r\n}\r\n\r\n<\/pre>\n<p>The console was throwing an error and rightfully so &#8211; time was undefined. This can be caused by anything but what first comes to mind would be some javascript, framework or library that isn&#8217;t utilized on that particular page &#8211; happens a lot. Perhaps it&#8217;s only utilized on a gallery page, but the library in embedded globally throughout the site. <\/p>\n<p>So how do we get rid of it? Well at first I tried the handy dandy boolean check before the program is run:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\nif (!time) {\r\n  return;\r\n}\r\n\r\nif (time) {\r\n\r\n \/\/ ... a bunch of code ...\r\n \/\/ ... a bunch of code ...\r\n\r\n}\r\n\r\n<\/pre>\n<p>Well that didn&#8217;t work, same result. Let&#8217;s try comparing it against the data type in question &#8211; yes, &#8220;undefined&#8221;.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\nif (time === &quot;undefined&quot;) {\r\n  return;\r\n}\r\n\r\nif (time) {\r\n\r\n \/\/ ... a bunch of code ...\r\n \/\/ ... a bunch of code ...\r\n\r\n}\r\n\r\n<\/pre>\n<p>Nope, that didn&#8217;t work &#8211; so, this is where typeof solves the issue &#8211; straight up. This time, we&#8217;ll output the variable by logging it. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\nif (typeof item === 'undefined') {\r\n        return;\r\n}\r\n\r\nif (item) {\r\n\r\n  console.log(item);\r\n}\r\n\r\n<\/pre>\n<p>So in the code above, we never get to logging the item. We use the return operator to end the program&#8217;s execution. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Javascript&#8217;s typeof operator is an essential debugging tool that many young programmers often miss. Countless times I&#8217;ve run into situations where my program wasn&#8217;t working because either my variable was unnoticeably coerced or I was just completely unaware of a data type. As mentioned in a previous post &#8211; a season programmer should know their &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fixing a network 404 error using typeof&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1150,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-475","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Fixing a network 404 error using typeof - Project Immerse<\/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.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fixing a network 404 error using typeof - Project Immerse\" \/>\n<meta property=\"og:description\" content=\"Javascript&#8217;s typeof operator is an essential debugging tool that many young programmers often miss. Countless times I&#8217;ve run into situations where my program wasn&#8217;t working because either my variable was unnoticeably coerced or I was just completely unaware of a data type. As mentioned in a previous post &#8211; a season programmer should know their &hellip; Continue reading &quot;Fixing a network 404 error using typeof&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/\" \/>\n<meta property=\"og:site_name\" content=\"Project Immerse\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-12T04:37:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-17T07:05:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/02\/javascript-type-of-operator.png\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"projectimmerse\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"projectimmerse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/\"},\"author\":{\"name\":\"projectimmerse\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"headline\":\"Fixing a network 404 error using typeof\",\"datePublished\":\"2019-02-12T04:37:56+00:00\",\"dateModified\":\"2020-06-17T07:05:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/\"},\"wordCount\":465,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/02\\\/javascript-type-of-operator.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/\",\"name\":\"Fixing a network 404 error using typeof - Project Immerse\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/02\\\/javascript-type-of-operator.png\",\"datePublished\":\"2019-02-12T04:37:56+00:00\",\"dateModified\":\"2020-06-17T07:05:55+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/02\\\/javascript-type-of-operator.png\",\"contentUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/02\\\/javascript-type-of-operator.png\",\"width\":750,\"height\":750,\"caption\":\"Fixing a network 404 error using typeof\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/fixing-a-network-404-error-using-typeof\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Fixing a network 404 error using typeof\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\",\"name\":\"Project Immerse\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\",\"name\":\"projectimmerse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"caption\":\"projectimmerse\"},\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/author\\\/projectimmerse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fixing a network 404 error using typeof - Project Immerse","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.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/","og_locale":"en_US","og_type":"article","og_title":"Fixing a network 404 error using typeof - Project Immerse","og_description":"Javascript&#8217;s typeof operator is an essential debugging tool that many young programmers often miss. Countless times I&#8217;ve run into situations where my program wasn&#8217;t working because either my variable was unnoticeably coerced or I was just completely unaware of a data type. As mentioned in a previous post &#8211; a season programmer should know their &hellip; Continue reading \"Fixing a network 404 error using typeof\"","og_url":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/","og_site_name":"Project Immerse","article_published_time":"2019-02-12T04:37:56+00:00","article_modified_time":"2020-06-17T07:05:55+00:00","og_image":[{"width":750,"height":750,"url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/02\/javascript-type-of-operator.png","type":"image\/png"}],"author":"projectimmerse","twitter_card":"summary_large_image","twitter_misc":{"Written by":"projectimmerse","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/#article","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/"},"author":{"name":"projectimmerse","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"headline":"Fixing a network 404 error using typeof","datePublished":"2019-02-12T04:37:56+00:00","dateModified":"2020-06-17T07:05:55+00:00","mainEntityOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/"},"wordCount":465,"commentCount":0,"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/02\/javascript-type-of-operator.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/","url":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/","name":"Fixing a network 404 error using typeof - Project Immerse","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/#primaryimage"},"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/02\/javascript-type-of-operator.png","datePublished":"2019-02-12T04:37:56+00:00","dateModified":"2020-06-17T07:05:55+00:00","author":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"breadcrumb":{"@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/#primaryimage","url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/02\/javascript-type-of-operator.png","contentUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/02\/javascript-type-of-operator.png","width":750,"height":750,"caption":"Fixing a network 404 error using typeof"},{"@type":"BreadcrumbList","@id":"https:\/\/www.projectimmerse.com\/blog\/fixing-a-network-404-error-using-typeof\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.projectimmerse.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Fixing a network 404 error using typeof"}]},{"@type":"WebSite","@id":"https:\/\/www.projectimmerse.com\/blog\/#website","url":"https:\/\/www.projectimmerse.com\/blog\/","name":"Project Immerse","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.projectimmerse.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5","name":"projectimmerse","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","caption":"projectimmerse"},"url":"https:\/\/www.projectimmerse.com\/blog\/author\/projectimmerse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/comments?post=475"}],"version-history":[{"count":6,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/475\/revisions"}],"predecessor-version":[{"id":1151,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/475\/revisions\/1151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media\/1150"}],"wp:attachment":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media?parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/categories?post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/tags?post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}