DEV Community

Discussion on: JS, method behind the madness.

Collapse
 
pentacular profile image
pentacular

I think your example code is a bit off

 const array = [6,-2,2,-7];
        array.sort((a,b)=>{
        a-b;
    });
Enter fullscreen mode Exit fullscreen mode

The arrow function you've supplied here always returns undefined.

I think you intended to write this instead:

  const array = [6,-2,2,-7];
  array.sort((a, b) => a - b);
Enter fullscreen mode Exit fullscreen mode

The moral of this story is to always test your examples. :)

Collapse
 
stefanovualto profile image
stefanovualto

Same with that one ;) (missing the return):

const array = [6,-2,2,-7];
        array.sort(function(a,b){
        a-b;
    });

to work it should be:

const array = [6,-2,2,-7];
        array.sort(function(a,b){
        return a-b;
    });
Collapse
 
katungi profile image
Daniel Dennis

Haha, Oh my God. I completely missed that. Thanks for the heads up