이 문서는 아직 자원 봉사자들이 한국어로 번역하지 않았습니다. 참여해서 번역을 마치도록 도와 주세요!
English (US)의 문서도 읽어보세요.

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 "Bananas" or "Apples", the program matches the values with either the case "Bananas" 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

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'switch statement' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Exports' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'switch statement' in that specification.
Living Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'Exports' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
default keyword in switch Yes Yes Yes Yes Yes Yes
default keyword with export ? ? ? ? ? ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
default keyword in switch Yes Yes Yes Yes Yes Yes Yes
default keyword with export ? ? ? ? ? ? ?

See also

문서 태그 및 공헌자

태그: 
 이 페이지의 공헌자: kasirivneni, fscholz, akshayjai1, jameshkramer, nmve, jsx, Pavan, webery
 최종 변경: kasirivneni,