DEV Community

Discussion on: 17 Pro JavaScript tricks you didn't know

Collapse
 
michi profile image
Michael Z
// Noobs:

let num = 15; 
let s = num.toString(); // number to string
let n = Number(s); // string to number

// Pro:

let num = 15;
let s = num + ""; // number to string
let n = +s; // string to number
Enter fullscreen mode Exit fullscreen mode

Sorry, but clever != pro. The "Noob" version much better reveals the intent of the code.

Collapse
 
rahxuls profile image
Rahul

I agree. But you know the pro version is better when you know many things about JS and doing big projects. I too had some doubt when writing this.

Collapse
 
zhnedyalkow profile image
Zhitomir Oreshenski

It is not readable and maintainable in the long run.

Thread Thread
 
rahxuls profile image
Rahul

Agree.

Collapse
 
hspaans profile image
Hans Spaans

I wouldn't say better as it reduces the readability and maintainability of the code a lot. Having a language with strict type casting may seem a hassle for some, but it saves you hours of debugging in the long run. It was one of the significant reasons for the development of TypeScript and as a goal for both PHP 7 and Python 4.

Thread Thread
 
rahxuls profile image
Rahul

People write codes with their read comfort ability.

Thread Thread
 
seanmclem profile image
Seanmclem

Pretty much the #1 rule when learning programming is to not only write for yourself but with future coders and teammates in mind.

Collapse
 
misobelica profile image
Mišo

Sorry, but I have to strongly disagree. Please don't make people think it's better to complicate things. The Noob version is actually a Pro especially in big projects. I already wrote it here dev.to/misobelica/comment/15b21

JS is not the best language even without these quirks. We should know them but consider them a very sad code smell and not a Pro version.

Thread Thread
 
rahxuls profile image
Rahul

I'm sorry sir. When i will post again something related to this i will surely keep this in mind. Currently i'm writing through my phone so it will be hard for me to do. I'm sorry once again

Collapse
 
lvegerano profile image
Luis Vegerano

I would never approve a PR with something like that.

Collapse
 
madza profile image
Madza

was going to say that too 😉

Collapse
 
x1k profile image
Asaju Enitan

I am not so cool with the "pro" code, def not advisable for team based projects

Collapse
 
aralroca profile image
Aral Roca

Same for

if(a!=123) // before // NOOBS

if(a^123) // after // PRO
Enter fullscreen mode Exit fullscreen mode

a^123 is not very clean IMO. Maybe a better one will be if(a !== 123)

Collapse
 
nucliweb profile image
Joan León

IMHO

if(a!=123) // before // NOOBS

if(a!==123) // after // PRO
Enter fullscreen mode Exit fullscreen mode
Collapse
 
migueloop profile image
Miguel Ruiz

Also we should think this is something that someone who comes from another Programming Language won't understand easily, just for saving 1 character

Collapse
 
aralroca profile image
Aral Roca • Edited

And it's not correct for decimal numbers.

Boolean(122^123) // true
Boolean(123.1^123) // false -> is not correct, should be true 123.1 !== 123
Boolean(123^123) // false
Enter fullscreen mode Exit fullscreen mode

Much better to use the !== operator.