Do We Need Semicolon at the End

Do we need semicolon at the end?

The concept is known as JavaScript Semicolon Insertion or "Automatic Semicolon Insertion". This blog post: JavaScript Semicolon Insertion: Everything you need to know (archived from the original) outlines the concept well in an understandable manner using examples under the headings:

  • Where Semicolons are Allowed
  • Where Semicolons May be Omitted
  • The rules

It even digs into the official ECMAScript specification about the topic.

Should I add a semi-colon at the end of a include statement?

No you shouldn't.


Why?

#xxxxxx are preprocessor directives. The former are part of a separate step in the compilation process. The preprocessor analyses the code before the actual compilation of code starts. All the directives are resolved before actual code is generated by normal C++ statements.

Summary:

  • Most C/C++ statements need to be ended with a semicolon ;. It's part of the C/C++ standard.
  • Preprocessor directives do not need to be ended by a ; or any other symbol.

Do we need a semicolon after function declaration?

A function declaration does not need (and should not have) a semicolon following it:

function test(o) {
}

However, if you write a function as an expression, like the variable initializer below, then the statement should be terminated with a semicolon, just like any other statement would be:

var a = function test(o) {
};

See more about constructor vs declaration(statement) vs expression.

Why must I put a semicolon at the end of class declaration in C++?

The full syntax is, essentially,

class NAME { constituents } instances ;

where "constituents" is the sequence of class elements and methods, and "instances" is a comma-separated list of instances of the class (i.e., objects).

Example:

class FOO {
int bar;
int baz;
} waldo;

declares both the class FOO and an object waldo.

The instance sequence may be empty, in which case you would have just

class FOO {
int bar;
int baz;
};

You have to put the semicolon there so the compiler will know whether you declared any instances or not.

This is a C compatibility thing.

Why do { } while(condition); needs semicolon at the end of it but while(condition) {} doesn't?

You put semicolon after all statements, except the block statement. This is the reason that you place it after the while in do while, but not after the block in the while {...}.

You also use it to terminate almost all declarations. The only exceptions I can think about at the moment is function bodies, and namespace bodies in C++.

Do you recommend using semicolons after every statement in JavaScript?

Yes, you should use semicolons after every statement in JavaScript.

Why should I use a semicolon after every function in javascript?

Semicolons after function declarations are not necessary.

The grammar of a FunctionDeclaration is described in the specification as this:

function Identifier ( FormalParameterListopt ) { FunctionBody }

There's no semicolon grammatically required, but might wonder why?

Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.

FunctionDeclarations are evaluated before the code enters into execution, hoisting is a common word used to explain this behaviour.

The terms "function declaration" and "function statement" are often wrongly used interchangeably, because there is no function statement described in the ECMAScript Specification, however there are some implementations that include a function statement in their grammar, -notably Mozilla- but again this is non-standard.

However, semicolons are always recommended where you use FunctionExpressions. For example:

var myFn = function () {
//...
};

(function () {
//...
})();

If you omit the semicolon after the first function in the above example, you will get completely undesired results:

var myFn = function () {
alert("Surprise!");
} // <-- No semicolon!

(function () {
//...
})();

The first function will be executed immediately, because the parentheses surrounding the second one will be interpreted as the Arguments of a function call.

Recommended lectures:

  • Named function expressions demystified (great article)
  • Explain JavaScript’s encapsulated anonymous function syntax (more on FunctionDeclaration vs FunctionExpression)

Should I place a semicolon at the end of my jQuery statement?

Always put the semicolon. Not only is it more cross-browser compatible, it's easier to minify (removing newlines is part of that).

Why is a semicolon required at end of line?

Take a look at this example of chained function calls.

a.push(['test'])(function() {alert('poop')})()

Look familiar? This is how the compiler/interpreter views your code.

Detail

Here is a portion of the grammar used to describe call expressions.


CallExpression :
MemberExpression Arguments
CallExpression Arguments
CallExpression [ Expression ]
CallExpression . IdentifierName

Essentially each group (...) is considered as Arguments to the original MemberExpression a.push.

a.push (['test'])                // MemberExpression Arguments 
(function() {alert('poop')}) // Arguments
() // Arguments

Or more formally


CallExpression(
CallExpression(
CallExpression(
MemberExpression( a.push ),
Arguments( (['test']) )
),
Arguments( (function() {alert('poop')}) )
),
Arguments( () )
)


Related Topics



Leave a reply



Submit