9

I'm reading this from the exploringjs about ES6

17.1.2 Single default export

There can be a single default export. For example, a function:

//------ myFunc.js ------   
export default function () { ··· } // no semicolon!

//------ main1.js ------  
import myFunc from 'myFunc';
myFunc();

Or a class:

//------ MyClass.js ------  
export default class { ··· } // no semicolon!

//------ main2.js ------  
import MyClass from 'MyClass';
let inst = new MyClass();

Note that there is no semicolon at the end if you default-export a function or a class (which are anonymous declarations).

Why do you not use a semi-colon at the end of the export default declaration? I thought you end all statements with semi-colons?

Community
  • 1
  • 1
Jwan622
  • 11,015
  • 21
  • 88
  • 181

3 Answers3

16

Why don't you need a semicolon?

Because the grammar doesn't define a semicolon there:

export default HoistableDeclaration
export default ClassDeclaration
export default [lookahead ∉ {function, class}] AssignmentExpression ;

(unless you have an expression)

I thought you end all statements with semi-colons?

That's not true at all. Have you ever put a semicolon after a block? If someone writes

if (...) {

};

then it is by mistake.

It may seem that all statements are terminated by semicolons, because at the end / bottom of most statements, you end up having an ExpressionStatement or empty statement, which both are terminated by semicolons.

Besides that, an ExportDeclaration is not a statement.

Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    Is a block considered a statement? – Tyler Nov 03 '15 at 08:07
  • Yes: http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-statements-and-declarations. Otherwise you would not be able to use it in an `if` statement. – Felix Kling Nov 03 '15 at 14:25
5

I thought you end all statements with semi-colons?

Yes, but declarations are no statements. This is not specific for exports, you don't put semicolons after normal function declarations either.

Btw, in statements you don't actually need semicolons, as JavaScript has automatic semicolon insertion - it only is good practise.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

The spec specifies:

Most ECMAScript statements and declarations must be terminated with a semicolon. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

So, IMO, if in an ES6 environment, and (especially) if you are working on server-side JavaScript, feel free to omit semi-colons in situations where it is obvious there will be no ill-effect.

Also see this article.

This is a good example of withholding semi-colons.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115