{"id":2560,"date":"2016-02-11T13:25:41","date_gmt":"2016-02-11T13:25:41","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=2560"},"modified":"2016-02-11T13:29:47","modified_gmt":"2016-02-11T13:29:47","slug":"why-doesnt-window-something-throw-an-error","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/why-doesnt-window-something-throw-an-error\/","title":{"rendered":"Why doesn&#8217;t window.something throw an error?"},"content":{"rendered":"<blockquote><p>In this StackOverflow question I asked why doesn&#8217;t window.something throw an error<\/p><\/blockquote>\n<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[2560]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts, I will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>As you may know, I&#8217;m really into Ionic framework lately\u00a0and am helping out on StackOverflow with the knowledge I gained so far with the framework. I&#8217;m currently <strong>#3<\/strong> in the <a href=\"http:\/\/stackoverflow.com\/tags\/ionic\/topusers\">top All time answerers<\/a> list.<\/p>\n<p>I actually asked <a href=\"http:\/\/stackoverflow.com\/questions\/31547789\/why-am-i-getting-unexpected-token-u0000-when-using-npm-install-g-package\/31635673\">this question<\/a>\u00a0myself:<\/p>\n<p>I&#8217;m using an AdMob plugin in my Ionic application, and one way, as you can see in <a href=\"https:\/\/github.com\/floatinghotpot\/cordova-admob-pro\" rel=\"nofollow\">the documentation<\/a>, to test if the plugin is properly loaded is to do:<\/p>\n<pre class=\"lang:default decode:true\">if(AdMob) {\r\n    \/\/\/other config code...\r\n}<\/pre>\n<p>Now, this works perfectly fine <strong>on a device<\/strong>. However, it doesn&#8217;t work <strong>in the browser<\/strong>; it throws an error in the console log: <code>AdMob is not defined<\/code>.<\/p>\n<p>I have found a solution to test the existence of plugin like this (without throwing an error in the console):<\/p>\n<pre class=\"lang:default decode:true\">if (window.AdMob){...}<\/pre>\n<p>And I have seen this usage on multiple questions here on StackOverflow. However, I wasn&#8217;t able to find an explanation to as <strong>why<\/strong> this doesn&#8217;t throw an error.<\/p>\n<p>I have a vague reasoning to as why this would be so, but I would really appreciate it if someone experienced could explain it in more detail.<\/p>\n<p><strong>edit<\/strong>: I made additional tests like this:<\/p>\n<pre class=\"lang:default decode:true\">var a = \"hi\";\r\nconsole.log(a); \/\/shows \"hi\"\r\nconsole.log(b); \/\/throws an error that b is not defined\r\n\r\nvar c = {};\r\nc.b = \"hi again\";\r\nconsole.log(c.b); \/\/shows \"hi again\" as expected\r\n\r\n\/\/and now for the grand finale\r\nconsole.log(c.something);\/\/doesn't throw an error, please explain to me in more detail why?<\/pre>\n<p><a href=\"http:\/\/stackoverflow.com\/a\/35334454\/534755\">The answer<\/a>\u00a0by <a href=\"http:\/\/stackoverflow.com\/users\/157247\/t-j-crowder\">T.J. Crowder<\/a>\u00a0was:<\/p>\n<blockquote>\n<blockquote><p>I wasn&#8217;t able to find an explanation to as why this doesn&#8217;t throw an error.<\/p><\/blockquote>\n<p>In the first example, you&#8217;re trying to read the value of a completely undefined identifier. In the second example, you&#8217;re trying to read a property from an object that the object may not have.<\/p>\n<p>Trying to read the value of an undefined identifier is a <code>ReferenceError<\/code>; the JavaScript engine has no idea what that identifer is. In contrast, trying to read the value of a property that the object doesn&#8217;t have yields the value <code>undefined<\/code>.<\/p>\n<p>It&#8217;s just how the language is designed, where Brendan Eich drew the line: It&#8217;s okay to read the value of a non-existant property from an object, but not okay to read the value of an undeclared identifier.<\/p>\n<p>I should point out a third option: <code>typeof<\/code>. You&#8217;re allowed to provide an undefined identifier as the operand to <code>typeof<\/code>:<\/p>\n<pre class=\"default prettyprint prettyprinted\"><code><span class=\"kwd\">if<\/span> <span class=\"pun\">(<\/span><span class=\"kwd\">typeof<\/span> <span class=\"typ\">AdMob<\/span> <span class=\"pun\">===<\/span> <span class=\"str\">\"undefined\"<\/span><span class=\"pun\">)<\/span><\/code><\/pre>\n<p>That won&#8217;t throw a <code>ReferenceError<\/code> even if <code>AdMob<\/code> is undeclared; instead, <code>typeof<\/code> will yield <code>\"undefined\"<\/code>. (It will also yield <code>\"undefined'<\/code> if <code>AdMob<\/code> is a declared identifier with the value <code>undefined<\/code> in it.)<\/p>\n<p>In a comment on another answer, you said:<\/p>\n<blockquote><p>&#8230;it would just indeed help to see the exact official specification which confirms this.<\/p><\/blockquote>\n<p>That would be the <a href=\"http:\/\/www.ecma-international.org\/ecma-262\/6.0\/index.html\" rel=\"nofollow\">ECMAScript specification<\/a>, specifically <a href=\"http:\/\/www.ecma-international.org\/ecma-262\/6.0\/index.html#sec-getvalue\" rel=\"nofollow\">\u00a76.2.3.1<\/a> for throwing a <code>ReferenceError<\/code> on an unresolvable symbol, and <a href=\"http:\/\/www.ecma-international.org\/ecma-262\/6.0\/index.html#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver\" rel=\"nofollow\">\u00a79.1.8<\/a> for returning <code>undefined<\/code> for a property that doesn&#8217;t exist. But I should warnin you that the spec, especially this 6th edition spec, is <strong>very<\/strong> heavy going. \ud83d\ude42<\/p><\/blockquote>\n<blockquote class=\"twitter-tweet\" data-width=\"550\">\n<p lang=\"en\" dir=\"ltr\">Why doesn\u2019t <a href=\"https:\/\/twitter.com\/hashtag\/window?src=hash\">#window<\/a>.something throw an <a href=\"https:\/\/twitter.com\/hashtag\/error?src=hash\">#error<\/a> in <a href=\"https:\/\/twitter.com\/Ionicframework\">@ionicframework<\/a>? <a href=\"https:\/\/twitter.com\/hashtag\/javascript?src=hash\">#javascript<\/a> <a href=\"https:\/\/t.co\/e0vMYiHJAU\">https:\/\/t.co\/e0vMYiHJAU<\/a> <a href=\"https:\/\/t.co\/lPHcStpDA5\">pic.twitter.com\/lPHcStpDA5<\/a><\/p>\n<p>&mdash; Nikola Bre\u017enjak (@HitmanHR) <a href=\"https:\/\/twitter.com\/HitmanHR\/status\/697774388368838657\">February 11, 2016<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this StackOverflow question I asked why doesn&#8217;t window.something throw an error I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2561,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,35],"tags":[],"class_list":["post-2560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ionic","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=2560"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2560\/revisions"}],"predecessor-version":[{"id":2563,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2560\/revisions\/2563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/2561"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=2560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=2560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=2560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}