The compiler threw me an error when I tried:
['a', 'b', 'c'].forEach(function (x) { if (x == 'b') { break //error message: Can't have 'break' outside of loop } })
Valid syntax:
var x = ['a', 'b', 'c']; for (var i = 0; i < x.length; i++) { if (x[i] == 'b') { break } }
So why?
forEachmay make you believe that you are in the context of a cycle for, but it’s not.
forEach
for
This is just a method that runs for each of the elements in the array. Thus, inside the function you can control only the current iteration, but in no way can you cancel or break out of the method subscription for other elements of the array.
An explanation of your question was well given by @Wim Hollebrandse.
If you want to break the loop, try using some instead forEach:
['a', 'b', 'c'].some(function (x) { if (x == 'b') { return true; } });
This is because you are in function. Keyword breaknot available here (out of loop)
break
Because it is a method in the Array prototype.
To exit, throw an exception.
Source: https://habr.com/ru/post/1620591/More articles:Why is code coverage zero with Scalatest, Maven and cobertura? - scalaTensorFlow Compute Caching - tensorflowhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1620588/how-can-i-apply-dynamic-date-formats-to-multiple-types-in-elasticsearch&usg=ALkJrhgtbNDWOt_uWMW1vBpkYpCcqJfz5QЗаголовок UIButton не отображается при применении UIVibrancyEffect - iosself join in update query с транзитивными данными - sqlRefresh column value for all rows of a table, where column value is Null? - sqljQuery mobile datepicker не работает o нажатие текстового поля - jqueryAccess to `self` object through parameters - ruby | fooobar.comReplace print statements in Python 2.7 - pythonMutant version of the tap - or a way of expressing pipes? - ruby | fooobar.comAll Articles