Optional chaining:
Optional chaining has made life of javaScript programmer easy by handling the checks using the syntax ?.
However, ...
For further actions, you may consider blocking this person and/or reporting abuse
Umm.. this is interesting.
Would you like to share what
~~
does in the solution above and how does it fix the unsafe optional chaining ?~
is a bitwise NOT operator - flipping all the bits in a number. Flipping them twice gets you back to the original number. Using it withundefined
(what you'll get from the optional chain iforder
is missing) or any falsy value - is the same as using it with 0I am making the presumption here that
order
will be an integer. The solution I suggest will also remove the decimal part from anyorder
valuePresumably you are using optional chaining here as you are expecting
a
orb
to possibly beundefined
ornull
? If this is not the case, you don't even need optional chaining and could simply use:Hey Jon, yes I am assuming the values to null or undefined, and that's the reason why I am using optional changing..
However, using your approach wouldn't
~~undefinded
break the code ? 🤔As I said,
~~undefined
gives0
umm, interesting way.. never used it though, however not sure if this is the best practise or not..
Best practises should always be challenged and questioned
Agreed, there's always more then one to do the stuff, whatever works best !
For someone interested for the answers,
One of the solution I got from stackOverflow was: