Insertion sort in JavaScript
Eliminating some unnecessary code

Some programmers find break statements to be problematic. If you'd like to know more, here is a programmers stack exchange question about breaks and continues.

You could easily eliminate your use of break by just changing the inner loop to the following:

for (var j = i - 1; j &gt;= 0 &amp;&amp; array[j] &gt; currentItem; j--) {
  array[j + 1] = array[j];
}


This shortens your code a bit and condenses all the control flow logic of you loop into one spot.



You could shorten your code even further by eliminating the conditional j &gt;= 0:

for (var j = i - 1; array[j] &gt; currentItem; j--) {
  array[j + 1] = array[j];
}


However, this might obfuscate the code a bit.

Note: This works because array[-1] will return undefined and undefined &gt; myVar will always evaluate to false - ECMAScript Standard