
{"id":4519,"date":"2019-03-22T21:47:43","date_gmt":"2019-03-23T01:47:43","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4519"},"modified":"2019-03-22T22:06:20","modified_gmt":"2019-03-23T02:06:20","slug":"javascript-implicit-semicolon-is-not-your-friend","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4519","title":{"rendered":"Javascript: implicit semicolon is NOT your friend"},"content":{"rendered":"\n<p>Today I spent several hours trying to find out why this code does not work:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function doit() {<br>     return <br>         someObject<br>             .method1()<br>             .method2();<br> }<\/pre>\n\n\n\n<p>but this one does:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function doit() {<br>     let temp;<br>     return temp =<br>         someObject<br>             .method1()<br>             .method2();<br> }<\/pre>\n\n\n\n<p>It involved some rather complicated promises and what-not, so I was tossing them around, trying to figure out where it gets stuck. The answer, however, is rather simple: unfortunate code formatting. When <code>return<\/code> is on a line by itself, Javascript inserts an implicit semicolon after it,  and completely ignores subsequent code, quietly returning <code>undefined<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function doit() {<br>     return <span style=\"color:red;font-size:large;background-color:yellow\">;<\/span><br>         someObject<span style=\"color:#F44\">;<\/span><br>             .method1()<span style=\"color:#F44\">;<\/span><br>             .method2();<br> }<\/pre>\n\n\n\n<p>Porting formatting habits from one language to another may be dangerous: not all whitespace is created equal. It is kind of obvious with Python, but Javascript may be tricky as well.<\/p>\n\n\n\n<p>Of course, this happened before to other people. There is even a note in some <a href=\"http:\/\/crockford.com\/javascript\/code.html\">coding conventions<\/a> that reads &#8220;<em>The return value expression must start on the same line as the\u00a0<\/em><code><em>return<\/em><\/code><em>\u00a0keyword in order to avoid semicolon insertion<\/em>&#8220;. In fact, I knew about the implicit semicolon, but I did not connect the dots, I was too busy debugging my promises \ud83d\ude42<\/p>\n\n\n\n<p>That&#8217;s all folks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I spent several hours trying to find out why this code does not work: function doit() { return someObject .method1() .method2(); } but this one does: function doit() { <a href=\"https:\/\/ikriv.com\/blog\/?p=4519\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4,15],"tags":[],"class_list":["entry","author-ikriv","post-4519","post","type-post","status-publish","format-standard","category-hack","category-webdev"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4519"}],"version-history":[{"count":4,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4519\/revisions"}],"predecessor-version":[{"id":4528,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4519\/revisions\/4528"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}