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 "another-module.js"
import cube from 'my-module';
// without default export we would need to write: import { cube } from 'my-module'
console.log(cube(3)); // 27

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'switch statement' in that specification.
ECMAScript (ECMA-262)
The definition of 'Exports' in that specification.

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
default keyword in switchChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 4Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
default keyword with exportChrome Full support 61Edge Full support 16
Full support 16
Full support 15
Disabled
Disabled From version 15: this feature is behind the Experimental JavaScript Features preference.
Firefox Full support 60
Full support 60
No support 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.
IE No support NoOpera Full support 47Safari Full support 10.1WebView Android No support NoChrome Android Full support 61Firefox Android Full support 60
Full support 60
No support 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.
Opera Android Full support 44Safari iOS Full support 10.3Samsung Internet Android Full support 8.0nodejs Full support 13.2.0
Notes
Full support 13.2.0
Notes
Notes Modules must either have a filename ending in .mjs, or the nearest parent package.json file must contain "type": "module". See Node's ECMAScript Modules documentation for more details.
Full support 12.0.0
Notes Disabled
Notes Modules must either have a filename ending in .mjs, or the nearest parent package.json file must contain "type": "module". See Node's ECMAScript Modules documentation for more details.
Disabled From version 12.0.0: this feature is behind the --experimental-modules runtime flag.
Full support 8.5.0
Notes Disabled
Notes Module filenames must end with .mjs, not .js. See Node's ECMAScript Modules documentation for more details.
Disabled From version 8.5.0: this feature is behind the --experimental-modules runtime flag.

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also