{"id":2610,"date":"2017-06-19T09:00:55","date_gmt":"2017-06-19T03:30:55","guid":{"rendered":"https:\/\/code4developers.com\/?p=2610"},"modified":"2018-09-04T20:50:19","modified_gmt":"2018-09-04T15:20:19","slug":"semicolon-in-javascript","status":"publish","type":"post","link":"https:\/\/code4developers.com\/semicolon-in-javascript\/","title":{"rendered":"Importance of Semicolon [;] In JavaScript"},"content":{"rendered":"<p>It is said that in JavaScript semicolons are not compulsory. It&#8217;s true, because JavaScript automatically inserts a semicolons at required place and it is know as &#8220;<strong>Automatic Semicolon Insertion&#8221;<\/strong>. This behavior of JavaScript confuses us a lot. This article is written for those who have just started writing JavaScript and for those who don\u2019t know the &#8220;<strong>Automatic insertion of semicolon in JavaScript<\/strong>&#8220;.<\/p>\n<p><!--more--><\/p>\n<pre class=\"theme:github lang:js decode:true\">function function1() {  \r\n    return {  \r\n        a: 10  \r\n    };  \r\n} \r\n\r\nfunction function2() {   \r\n    return \r\n    {  \r\n        a: 10  \r\n    };  \r\n}<\/pre>\n<p>At first look, the code given above looks same but look at the output given below.<\/p>\n<pre class=\"theme:github lang:default decode:true\">Console.log(function1());\r\n\r\nOutput:\r\n\r\nObject { a: 10 }\r\n\r\n<\/pre>\n<pre class=\"theme:github lang:default decode:true\">Console.log(function2());\r\n\r\nOutput:\r\n\r\nundefined<\/pre>\n<p>It is quite surprising that function2() returns undefined without any error being thrown.<\/p>\n<p>The reason behind the function returning undefined is the fact that in JavaScript semicolons are optional (although it is not a good practice to ignore them). Hence, when the line with the return statement is executed in function2(), it automatically places a semicolon immediately at the end of the return statement. In this case, no error is thrown as the remainder is perfectly valid, even though it is not invoked and doesn&#8217;t do anything.<\/p>\n<p>This behavior also suggests following the convention of placing an opening curly brace at the end of the same line and not at the new line.<\/p>\n<h4 id=\"following-statements-must-be-terminated-with-the-semicolons\"><strong>Following statements must be terminated with the semicolons<\/strong><\/h4>\n<ul>\n<li>empty statement<\/li>\n<li>let<\/li>\n<li>const<\/li>\n<li>import, and export<\/li>\n<li>expression statement<\/li>\n<li>var statement<\/li>\n<li>debugger statement<\/li>\n<li>continue statement<\/li>\n<li>break statement<\/li>\n<li>return statement<\/li>\n<li>throw statement<\/li>\n<\/ul>\n<p><strong>Rules of automatic semicolon insertion<\/strong><\/p>\n<p>There are three basic rules of semicolon insertion, which are given below.<\/p>\n<ul>\n<li>When a token is encountered (LineTerminator or }) that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token, if one or more of the following conditions is true.\n<ul>\n<li>If the token is separated from the previous token by at least one LineTerminator.<\/li>\n<li>If the token is }.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"theme:github lang:default decode:true\">{  1  \r\n   2 } 3\r\n  \r\nThis will transform to \r\n\r\n{ 1 \/\/Meets first condition  \r\n  ;2 ;} 3; \/\/Meets second condition<\/pre>\n<ul>\n<li>When the end of the input stream of tokens is encountered and the parser is unable to parse the input token stream as a single complete ECMAScript <em>Script<\/em> or <em>Module<\/em>, a semicolon is automatically inserted at the end of the input stream.\n<pre class=\"theme:github lang:default decode:true\">a = b\r\n++c\r\n\/\/This will transform to\r\na = b;\r\n++c;\r\n<\/pre>\n<p>Note, the token ++ is not treated as a postfix operator applying to the variable b. Because a LineTerminator occurs between b and ++.<\/li>\n<\/ul>\n<ul>\n<li>In this case, semicolon will be inserted, if the token is allowed by some production of the grammar, but it is <em>restricted production<\/em>.<\/li>\n<\/ul>\n<pre class=\"theme:github lang:default decode:true\">UpdateExpression :  \r\n  \r\nLeftHandSideExpression [no LineTerminator here] ++  \r\n  \r\nLeftHandSideExpression [no LineTerminator here] --  \r\n  \r\nContinueStatement :  \r\n  \r\ncontinue ;  \r\n  \r\ncontinue [no LineTerminator here] LabelIdentifier ;  \r\n  \r\nBreakStatement :  \r\n  \r\nbreak ;  \r\n  \r\nbreak [no LineTerminator here] LabelIdentifier ;  \r\n  \r\nReturnStatement :  \r\n  \r\nreturn ;  \r\n  \r\nreturn [no LineTerminator here] Expression ;  \r\n  \r\nThrowStatement :  \r\n  \r\nthrow [no LineTerminator here] Expression ;  \r\n  \r\nArrowFunction :  \r\n  \r\nArrowParameters [no LineTerminator here] =&gt; ConciseBody  \r\n  \r\nYieldExpression :  \r\n  \r\nyield [no LineTerminator here] * AssignmentExpression  \r\n  \r\nyield [no LineTerminator here] AssignmentExpression<\/pre>\n<p>The example, which we have seen at the start of the article is the best for understanding ReturnStatement (<em>restricted production<\/em>).<\/p>\n<p><a href=\"https:\/\/code4developers.com\/category\/javascript\/\">Interested in JavaScript? Click here to read more JavaScript articles in Code4Developers<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is said that in JavaScript semicolons are not compulsory. It&#8217;s true, because JavaScript automatically inserts a semicolons at required place and it is know as &#8220;Automatic Semicolon Insertion&#8221;. This&hellip;<\/p>\n","protected":false},"author":4,"featured_media":3127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"powerkit_post_featured":[],"class_list":{"0":"post-2610","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8NAi4-G6","jetpack-related-posts":[{"id":3321,"url":"https:\/\/code4developers.com\/spread-syntax-in-javascript\/","url_meta":{"origin":2610,"position":0},"title":"Spread syntax (three dots) in JavaScript","author":"Yatendrasinh Joddha","date":"March 21, 2018","format":false,"excerpt":"Spread syntax which is used by typing three dots (...) in JavaScript. It allows an array expression or string or anything which can be iterating to be expanded in places where zero or more arguments for function calls\u00a0or elements for array are expected. It can also be used for an\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":7069,"url":"https:\/\/code4developers.com\/ways-to-use-generator-functions-in-javascript\/","url_meta":{"origin":2610,"position":1},"title":"Ways to use Generator Functions in JavaScript","author":"Arif Khoja","date":"May 15, 2020","format":false,"excerpt":"In this Article we will see below points Generator Functions Recap: Functions How does it work? 4 ways to use Generator Functions with Examples Advantages of using Generator function What are Generator Functions? Ever imagined what would happen if a function had an infinite input or output to deal with?\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"Functional-programming-js","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2020\/04\/avatar-js.png?fit=512%2C512&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2975,"url":"https:\/\/code4developers.com\/event-capturing-bubbling-javascript\/","url_meta":{"origin":2610,"position":2},"title":"Event Capturing and Bubbling in JavaScript","author":"Yatendrasinh Joddha","date":"July 21, 2017","format":false,"excerpt":"The DOM has two approaches for object to sense events: first is top down approach and second is bottom up approach. Top down approach is called event capturing\u00a0whereas bottom up is known as event bubbling. Let\u2019s start with example where you have one html which contains <div>, <p> under <div>,\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3106,"url":"https:\/\/code4developers.com\/javascript-template-literals\/","url_meta":{"origin":2610,"position":3},"title":"Javascript: What Are Template Literals?","author":"Arif Khoja","date":"September 30, 2017","format":false,"excerpt":"ES6 introduced two types of literals: Template Literals and Tagged Template Literals to tag Template Literals. In this post, we cover both of them in depth! In ES6, two types of literals were introduced: Template Literals for string concatenation and expression embedding in a string Tagged Template Literals to tag\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/thumb-1-e1498942297714.png?fit=240%2C269&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":14692,"url":"https:\/\/code4developers.com\/demystifying-javascript-tree-shaking\/","url_meta":{"origin":2610,"position":4},"title":"Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size","author":"Yatendrasinh Joddha","date":"October 17, 2023","format":false,"excerpt":"In the ever-evolving world of web development, optimizing the performance of your JavaScript applications is a key concern. One effective technique to achieve this is called \"tree shaking.\" In this article, we'll delve into the concept of tree shaking, explain how it works, and provide a detailed example to illustrate\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3311,"url":"https:\/\/code4developers.com\/default-parameter-in-a-javascript-function\/","url_meta":{"origin":2610,"position":5},"title":"Default Parameter in JavaScript function","author":"Yatendrasinh Joddha","date":"March 17, 2018","format":false,"excerpt":"In any programming language we often require having default parameter or default value for the parameter in a function. JavaScript allow us to initialize parameter a default value. If you are not passing any value to the parameter, then the default value of the parameter will be undefined. Let's consider\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/2610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/comments?post=2610"}],"version-history":[{"count":12,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/2610\/revisions"}],"predecessor-version":[{"id":3896,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/2610\/revisions\/3896"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media\/3127"}],"wp:attachment":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media?parent=2610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/categories?post=2610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/tags?post=2610"},{"taxonomy":"powerkit_post_featured","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/powerkit_post_featured?post=2610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}