• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

Importance of Semicolon [;] In JavaScript

  • Yatendrasinh Joddha
  • June 19, 2017
  • 2 minute read
javascript
Total
0
Shares
0
0
0

It is said that in JavaScript semicolons are not compulsory. It’s true, because JavaScript automatically inserts a semicolons at required place and it is know as “Automatic Semicolon Insertion”. 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’t know the “Automatic insertion of semicolon in JavaScript“.

function function1() {  
    return {  
        a: 10  
    };  
} 

function function2() {   
    return 
    {  
        a: 10  
    };  
}

At first look, the code given above looks same but look at the output given below.

Console.log(function1());

Output:

Object { a: 10 }

Console.log(function2());

Output:

undefined

It is quite surprising that function2() returns undefined without any error being thrown.

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’t do anything.

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.

Following statements must be terminated with the semicolons

  • empty statement
  • let
  • const
  • import, and export
  • expression statement
  • var statement
  • debugger statement
  • continue statement
  • break statement
  • return statement
  • throw statement

Rules of automatic semicolon insertion

There are three basic rules of semicolon insertion, which are given below.

  • 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.
    • If the token is separated from the previous token by at least one LineTerminator.
    • If the token is }.
{  1  
   2 } 3
  
This will transform to 

{ 1 //Meets first condition  
  ;2 ;} 3; //Meets second condition
  • 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 Script or Module, a semicolon is automatically inserted at the end of the input stream.
    a = b
    ++c
    //This will transform to
    a = b;
    ++c;
    

    Note, the token ++ is not treated as a postfix operator applying to the variable b. Because a LineTerminator occurs between b and ++.

  • In this case, semicolon will be inserted, if the token is allowed by some production of the grammar, but it is restricted production.
UpdateExpression :  
  
LeftHandSideExpression [no LineTerminator here] ++  
  
LeftHandSideExpression [no LineTerminator here] --  
  
ContinueStatement :  
  
continue ;  
  
continue [no LineTerminator here] LabelIdentifier ;  
  
BreakStatement :  
  
break ;  
  
break [no LineTerminator here] LabelIdentifier ;  
  
ReturnStatement :  
  
return ;  
  
return [no LineTerminator here] Expression ;  
  
ThrowStatement :  
  
throw [no LineTerminator here] Expression ;  
  
ArrowFunction :  
  
ArrowParameters [no LineTerminator here] => ConciseBody  
  
YieldExpression :  
  
yield [no LineTerminator here] * AssignmentExpression  
  
yield [no LineTerminator here] AssignmentExpression

The example, which we have seen at the start of the article is the best for understanding ReturnStatement (restricted production).

Interested in JavaScript? Click here to read more JavaScript articles in Code4Developers

Yatendrasinh Joddha
Yatendrasinh Joddha

I have healthy knowledge and experience in Azure, O365 and ASP.Net with c#, MVC, WebApi along with client-side frameworks and libraries like JavaScript, JQuery, KnockoutJs, AngularJs, Angular, ReactJs, NodeJs

Views: 9,080

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
7 comments
  1. Idris says:
    July 29, 2017 at 12:02 pm

    Dear Jodha, Still confusing why second function not executed. Can you explain

    Reply
    1. Yatendrasinh Joddha says:
      July 29, 2017 at 1:12 pm

      In function2() there is nothing written after ‘return’ statement, thus JavaScript adds semicolon after return statement. That is why function2() returns undefined

      Reply
      1. Kim says:
        December 19, 2018 at 8:07 pm

        Hey Yatendrasinh, I feel that is a bad example to show the important of placing semicolon at the end but how this could cause an error. I didn’t understand that bit better about the javascript inserts the semicolon in the background.

        return {
        a: 10
        };

        and

        return
        {
        a: 10
        };

        I treat the whole return { a:10 }; as a whole, so when you are talking about at the end, I am thinking of return {a:10} and not the layout.

        Reply
  2. Himanshu says:
    August 1, 2017 at 4:30 pm

    Can you tell me how to access dynamic object in javascript

    Reply
  3. assam says:
    August 1, 2017 at 7:35 pm

    loved your subject of discussion. minor but important one. Please bring this kind of small small but important stuffs. Nice reading it is.

    Reply
  4. Idris says:
    April 17, 2018 at 12:14 pm

    But am not getting undefined while console function 2

    Reply
    1. Yatendrasinh Joddha says:
      April 17, 2018 at 12:18 pm

      It should return you undefined. Can you please put you code here. What and how you are trying?

      Reply

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • Getting Started with AWS Step Functions: Orchestration Made Simple

    Getting Started with AWS Step Functions: Orchestration Made Simple

    4 months ago
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    2 years ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    3 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d