DEV Community

Cover image for JS, method behind the madness.

JS, method behind the madness.

Daniel Dennis on September 08, 2020

Hi There, Welcome to my first Post ☄ It all started with this meme A friend of mine called me out as a now lover of JS. To be honest, ...
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

Collapse
 
alannato profile image
AlanNato

Before the world ends.
I hope I'll write a Hello 👋 World in Js.

Collapse
 
katungi profile image
Daniel Dennis

You really should, pretty neat language when you learn it.

Collapse
 
anarchy40 profile image
Anarchy

Said the C #eron...

Collapse
 
billy_de_cartel profile image
Billy Okeyo

Some good insights on that. I love your conclusion

Collapse
 
katungi profile image
Daniel Dennis

Thank you So much for the feedback

Collapse
 
saberhosneydev profile image
Saber Hosney

Are you sure microsoft didn't "sponsor" this article to increase awareness of Typescript
〜( ̄▽ ̄〜)

Collapse
 
katungi profile image
Daniel Dennis

Yikes, They skipped Florin Pop and came to me for a promotional Blog.

Collapse
 
gwutama profile image
Galuh Utama

Ah JavaScript. A jungle of landmines. There’s even a pitfall when it’s used for sorting numbers.

Collapse
 
agiesey profile image
AGiesey

Thanks for the article! JS has some weird gotchyas and it's nice to be reminded of specific examples.

Collapse
 
katungi profile image
Daniel Dennis

You are welcome ! I hope you learned a thing or 2

Collapse
 
tillern profile image
TILLERN • Edited

Javascript is just awesome

Collapse
 
katungi profile image
Daniel Dennis

Thank you Mary Anna. We love JS regardless