Don't Skip this JS tip! 🔥🚀

preview_player
Показать описание
We can convert JavaScript strings to numbers with the Number constructor and the parseInt function, but the fastest and preferred way of converting something into a number is using the Unary Plus.

👇 Follow Me On Social Media:
Рекомендации по теме
Комментарии
Автор

Im not a js expert, but in any other language you would make sure that the string can in fact be parsed into a number before trying to send it off to your database.

Also the performance benefit of doing '+s' here is ABSOLUTELY negligible and does not outweigh decreased readability and not being able to handle the error.

shurgars
Автор

I had learnt this when I was an intern, I appreciate u for what u do❤

Piyushraj
Автор

Thank you mentor. Still waiting on the rest of the Nest.js videos. We love you ❤

blaqhakym
Автор

This is perfect for hobby code, IMO, same as with !! For casting to booleans. If you're working on enterprise software however, please don't do this kind of thing, just be explicit. It's also doubtful that the supposed performance benefits of using these hacks are viable on the server side where JIT and other automatic engine optimizations are in place. For client side, optimization/minification build steps would also probably take care of this, so just leave it out and focus on writing some decent tests 😉

ShortFilmVD
Автор

I use this trick almost every time to the point where I don't even recall Number or parseInt. This is my go to approach

Captainofthethdivison
Автор

It feels wrong that the user can enter true, false, or null, and end up on a valid database request

dracenmarx
Автор

That's genious, thank U for this very good tip

UweGrimm
Автор

For readability and type safety, maybe not the best idea.

But if you don’t care about that: n*1 and ~~n also work

peqysyz
Автор

Ah yes, the millisecond saved here will really make a big difference when you have to wait 10 seconds to get the date from your query anyway

Hard to read code, which does absolutely no error checking, and actually produces a "valid" result even if the input is completely invalid, just to save some milliseconds, is not the way...

Eknoma
Автор

One reason this is “more correct” is that parseInt tries to find a number. So something like “12 Monkeys” will return 12, which is misleading. + will return NaN, which is correct.

mattburgess
Автор

While unary plus is faster but it's not clear and someone may accidently remove it thinking that it's not necessary to have a plus in this position,
I usually multiply by 1, and this may also look unnecessary but at least it makes me think for a while before attempting to remove it.

ahmad-murery
Автор

Speed is not an important criterion in parsing query strings if you are doing anything non-trivial with the result. Unary plus does have the advantage that it returns NaN if the string can't be completely parsed as a numeric value.

davidgillies
Автор

With “+id” it is less clear what is going on compared to “Number(id)”. Are we incrementing the id? Adding something to the id? Concatenating something to the id?

Because of this obfuscation I think this is a bad tip.

Code is made for humans, not computers.

dawid_dahl
Автор

As a backend dev:
wat
(Naw but ty this was helpful, i learned JS ~11 years ago so basically that was a different language lol)

forgetfulfunctor
Автор

but if at some place 2 '+' are mistyped then it will create a bug

yngeshraman
Автор

Do we any hake for vice-versa number(Integer) to string

RajKumar-zqcv
Автор

Does it work with scientific "e" notation?

ben_jammin
Автор

Pretty sure most databases convert this in a parameterized query?

Kayzewolf
Автор

I know this right now inner me I'm skillful 😂😂😂

yogeshvanzara
Автор

Whoever watches this in 2024: do not use numeric id‘s in databases. Their usage is historical. Use UUIDs instead.

failwell