2

I'm trying out JavaScript modules by enabling the "Experimental Web Platform" flag in Chrome Dev (v60, at time of writing).

When I try to import this module:

export default let foo = 10;

I get this error message:

Uncaught SyntaxError: Unexpected strict mode reserved word

It works fine without the default keyword. And this works fine too:

let a = 10;
export default a;

MDN doesn't explicitly say that the export default ... notation is allowed with let, var, const, etc. but they do give examples with functions like this:

export default function (…) { … }.

And variables like this:

export let name1 = …, name2 = …, …, nameN;

Question: Is it not possible with the current specification to export regular variables with the default keyword as can be done with functions and classes?

1 Answers1

4

export has strictly defined syntax, the proper syntax for default export is

export default expression;

let foo = 10 is not an expression but a statement. It can be used for named exports:

export let name1, name2, …, nameN;

For default export name is not needed.

Similarly to export default function (…) { … }, it should be

export default 10;
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • Ahh I see, didn't know I could just do `export default 10;`! But why do they have this example in [the MDN article](https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export)? `export let name1 = …, name2 = …, …, nameN;` –  Jun 02 '17 at 07:50
  • As the answer says, It is for *named exports*, not for *default* (`default` is actually the name). – Estus Flask Jun 02 '17 at 07:51
  • 3
    Also, it is not actually 'variables vs classes/functions' exports. `class Foo {...}` is a statement, so it can be named export, `export class Foo {...}`,. But it can also be an expression, both `export default class Foo {...}` and `export default class {...}` are ok. The same applies to functions. `var/let/const` statements aren't expressions. You can't use them anywhere where an expression is expected. Try something like `return let foo = 10` and you will get syntax error, too. – Estus Flask Jun 02 '17 at 07:59
  • Ah so a function definition like `function foo() {...}` can be both an expression and a statement. The `return let foo = 10` analogy is great –  Jun 02 '17 at 08:36