sort array by string length javascript

JavaScript
function sortByLength(arr) {
   return arr.sort((x,y) => x.length - y.length);
}array.sort(function(a, b){return b.length - a.length});
Source

Also in JavaScript: