default 키워드는 switch 구문과 export 구문에서 사용할 수 있습니다.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
구문
switch 구문 내에서는 다음과 같이 사용합니다.
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;]
}
export 구문 내에서는 다음과 같이 사용합니다.
export default nameN
설명
세부사항을 보시려면,
예제
switch에서 default 사용
아래 예제에서 expr 이 "오렌지" 또는 "사과"일 때, 프로그램은 값을 "오렌지" 또는 "사과"와 일치시키고 해당 명령문을 실행합니다. 기본(default) 키워드는 다른 경우에 도움이 되며 연관된 명령문을 실행합니다.
switch (expr) {
case '오렌지':
console.log('오렌지는 1000원입니다.');
break;
case '사과':
console.log('사과는 500원입니다.');
break;
default:
console.log('죄송합니다. ' + expr + '의 재고가 다 떨어졌습니다.');
}
export에서 default 사용
단일 값을 내보내거나 모듈의 기본 값이 필요한 경우, 기본으로 내보내기를 사용할 수 있습니다.
// module "my-module.js"
let cube = function cube(x) {
return x * x * x;
};
export default cube;
다른 스크립트에서 가져오는 건 간단합니다.
// 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
명세
브라우저 호환성
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
default keyword in switch | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
default keyword with export | Chrome Full support 61 | Edge
Full support
16
| Firefox
Full support
60
| IE No support No | Opera Full support 47 | Safari Full support 10.1 | WebView Android No support No | Chrome Android Full support 61 | Firefox Android
Full support
60
| Opera Android Full support 44 | Safari iOS Full support 10.3 | Samsung Internet Android Full support 8.0 | nodejs ? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- User must explicitly enable this feature.
- User must explicitly enable this feature.