{"id":11756,"date":"2014-02-11T13:05:23","date_gmt":"2014-02-11T07:35:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11756"},"modified":"2014-02-11T13:05:23","modified_gmt":"2014-02-11T07:35:23","slug":"javascripts-return-statement-gotcha","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/javascripts-return-statement-gotcha\/","title":{"rendered":"JavaScript&#8217;s return statement gotcha"},"content":{"rendered":"<p>Hi everyone,<\/p>\n<p>This blog is about how JavaScript parses your script (source code) when it encounters <strong>return<\/strong> statement. We can write code using any one style of following two:<\/p>\n<p>Style 1:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nfunction func() {<br \/>\n    return 6;<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>and<\/p>\n<p>Style 2:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nfunction func()<br \/>\n{<br \/>\n    return 6;<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>And people usually say that this is the matter of personal preference. But watch out! Here&#8217;s the catch. You can&#8217;t always put opening curly brace on next line just after <strong>return<\/strong> statement. For example, if you want to return an object from the function then the following code will not work:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nfunction func() {<br \/>\n    return<br \/>\n    {<br \/>\n        &quot;key&quot;: &quot;value&quot;<br \/>\n    };<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>It&#8217;ll throw <strong>SyntaxError<\/strong>. The reason is how the JavaScript interpret its source code file. As we all know semicolon at line end is optional, and in this optional case JavaScript parser tries to break the physical line to logical line using new line character, and thus problem begins. So the correct way is as follows:<\/p>\n<p>either this:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nfunction func() {<br \/>\n    return {<br \/>\n        &quot;key&quot;: &quot;value&quot;<br \/>\n    };<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>or like this:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nfunction func()<br \/>\n{<br \/>\n    return {<br \/>\n        &quot;key&quot;: &quot;value&quot;<br \/>\n    };<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>So folks, never put opening curly brace on next line just after <strong>return<\/strong> statement, whether you like it or not!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone, This blog is about how JavaScript parses your script (source code) when it encounters return statement. We can write code using any one style of following two: Style 1: [sourcecode language=&#8221;java&#8221;] function func() { return 6; } [\/sourcecode] and Style 2: [sourcecode language=&#8221;java&#8221;] function func() { return 6; } [\/sourcecode] And people usually [&hellip;]<\/p>\n","protected":false},"author":70,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[55,1314],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11756"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/70"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11756"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11756\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}