این ترجمه ناقص است. لطفاً در ترجمه این مقاله از انگلیسی کمک کنید.

The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.

دستور زبان

break [label];
label
Optional. Identifier associated with the label of the statement. If the statement is not a loop or switch, this is required.

تعریف

The break statement includes an optional label that allows the program to break out of a labeled statement. The break statement needs to be nested within the referenced label. The labeled statement can be any block statement; it does not have to be preceded by a loop statement.

مثال ها

تابع زیرین عبارت break داراست.عبارت وقتی پایان می یابد که حلقه یا loop به عدد ۳ برسد، و بعد از آن while loop when i is 3, and then returns the value 3 * x.

function testBreak(x) {
  var i = 0;

  while (i < 6) {
    if (i == 3) {
      break;
    }
    i += 1;
  }

  return i * x;
}

The following code uses break statements with labeled blocks. A break statement must be nested within any label it references. Notice that inner_block is nested within outer_block.

outer_block: {
  inner_block: {
    console.log('1');
    break outer_block; // breaks out of both inner_block and outer_block
    console.log(':-('); // skipped
  }
  console.log('2'); // skipped
}

The following code also uses break statements with labeled blocks but generates a Syntax Error because its break statement is within block_1 but references block_2. A break statement must always be nested within any label it references.

block_1: {
  console.log('1');
  break block_2; // SyntaxError: label not found
}

block_2: {
  console.log('2');
}

Specifications

Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition. Unlabeled version.
ECMAScript 3rd Edition (ECMA-262) Standard Labeled version added.
ECMAScript 5.1 (ECMA-262)
The definition of 'Break statement' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Break statement' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'Break statement' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support

See also

Document Tags and Contributors

Contributors to this page: omidrezaTavassoli
Last updated by: omidrezaTavassoli,