Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 965581 of default

  • Revision slug: Web/JavaScript/Reference/Statements/default
  • Revision title: default
  • Revision id: 965581
  • Created:
  • Creator: webery
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Statements")}}

The default keyword can be used in two situations in JavaScript: within a {{jsxref("Statements/switch", "switch")}} statement, or with an {{jsxref("Statements/export", "export")}} statement.

Syntax 语法

在{{jsxref("Statements/switch", "switch")}} 语句中使用:

switch (expression) {
  case value1:
    //当表达式的值和value1匹配执行这里的语句
    [break;]
  default:
    //当表达式的值没有匹配,执行这里的语句
    [break;]
}

With {{jsxref("Statements/export", "export")}} statement:

export default nameN 

Description 描述

更多细节,请看

  • {{jsxref("Statements/switch", "switch")}} 语句和
  • {{jsxref("Statements/export", "export")}} 语句页面.

例子

在switch语句中使用default

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 myFunction from 'my-module';
console.log(myFunction(3)); // 9

Specifications 规范

Specification Status Comment
{{SpecName('ES6', '#sec-switch-statement', 'switch statement')}} {{Spec2('ES6')}}  
{{SpecName('ES6', '#sec-exports', 'Exports')}} {{Spec2('ES6')}}  
{{SpecName('ESDraft', '#sec-switch-statement', 'switch statement')}} {{Spec2('ESDraft')}}  
{{SpecName('ESDraft', '#sec-exports', 'Exports')}} {{Spec2('ESDraft')}}  

Browser compatibility 浏览器兼容性

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Switch default {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Export default {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Switch default {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Export default {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

  • {{jsxref("Statements/export", "export")}}
  • {{jsxref("Statements/switch", "switch")}}

Revision Source

<div>{{jsSidebar("Statements")}}</div>

<p>The <strong>default keyword </strong>can be used in two situations in JavaScript: within a&nbsp;{{jsxref("Statements/switch", "switch")}} statement, or with an&nbsp;{{jsxref("Statements/export", "export")}} statement.</p>

<h2 id="Syntax">Syntax 语法</h2>

<p>在{{jsxref("Statements/switch", "switch")}} 语句中使用:</p>

<pre class="syntaxbox">
switch (expression) {
  case value1:
    //当表达式的值和value1匹配执行这里的语句
    [break;]
  default:
    //当表达式的值没有匹配,执行这里的语句
    [break;]
}</pre>

<p>With&nbsp;{{jsxref("Statements/export", "export")}} statement:</p>

<pre class="syntaxbox">
export default <em>nameN</em> </pre>

<h2 id="Description_描述">Description 描述</h2>

<p>更多细节,请看</p>

<ul>
 <li>{{jsxref("Statements/switch", "switch")}} 语句和</li>
 <li>{{jsxref("Statements/export", "export")}} 语句页面.</li>
</ul>

<h2 id="例子">例子</h2>

<h3 id="Using_default_in_switch_statements在switch语句中使用default">在switch语句中使用default</h3>

<p>In the following example, if <code>expr</code>&nbsp;evaluates to "Bananas" or "Apples", the program matches the values with either the case "Bananas" or "Apples" and executes the corresponding statement. The <code>default</code> keyword will help in any other case and executes the associated statement.</p>

<pre class="brush: js">
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 + ".");
}</pre>

<h3 id="Using_default_with_export">Using <code>default</code> with <code>export</code></h3>

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

<pre class="brush: js">
// module "my-module.js"
let cube = function cube(x) {
  return x * x * x;
}
export default cube;</pre>

<p>Then, in another script, it will be straightforward to import the default export:</p>

<pre class="brush: js">
// module "my-module.js"
import myFunction from 'my-module';
console.log(myFunction(3)); // 9
</pre>

<h2 id="Specifications">Specifications 规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-switch-statement', 'switch statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-exports', 'Exports')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-switch-statement', 'switch statement')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-exports', 'Exports')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility 浏览器兼容性</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Switch default</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Export default</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Switch default</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Export default</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Statements/export", "export")}}</li>
 <li>{{jsxref("Statements/switch", "switch")}}</li>
</ul>
Revert to this revision