W3cubDocs

/JavaScript

default

The default keyword can be used in two situations in JavaScript: within a switch statement, or with an export statement.

Syntax

Within a switch statement:

switch (expression) {
  case value1:
    //Statements executed when the result of expression matches value1
    [break;]
  default:
    //Statements executed when none of the values match the value of the expression
    [break;]
}

With export statement:

export default nameN 

Description

For more details see the

Examples

Using default in switch statements

In the following example, if expr evaluates to "Oranges" or "Apples", the program matches the values with either the case "Oranges" or "Apples" and executes the corresponding statement. The default keyword will help in any other case and executes the associated statement.

switch (expr) {
  case 'Oranges':
    console.log('Oranges are $0.59 a pound.');
    break;
  case 'Apples':
    console.log('Apples are $0.32 a pound.');
    break;
  default:
    console.log('Sorry, we are out of ' + expr + '.');
}

Using default with export

If you want to export a single value or need a fallback value for a module, a default export can be used:

// module "my-module.js"
let cube = function cube(x) {
  return x * x * x;
};
export default cube;

Then, in another script, it will be straightforward to import the default export:

// module "my-module.js"
import cube from 'my-module';  //default export gave us the liberty to say import cube, instead of import cube from 'my-module'
console.log(cube(3)); // 27

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
default keyword in switch Yes Yes 1 Yes Yes Yes
default keyword with export 61 16
16
15
Disabled
Disabled From version 15: this feature is behind the Experimental JavaScript Features preference.
60
60
54 — 60
Disabled
Disabled From version 54 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference. To change preferences in Firefox, visit about:config.
No 47 10.1
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
default keyword in switch Yes Yes Yes 4 Yes Yes Yes
default keyword with export No 61 Yes 60
60
54 — 60
Disabled
Disabled From version 54 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference. To change preferences in Firefox, visit about:config.
47 10.1 No
Server
Node.js
default keyword in switch Yes
default keyword with export ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/default