Your latter example would be the "implicit" coercion. Under the hood, I believe JavaScript is doing something similar to the !! way, right? So the result would be the same . I don't recall completely unfortunately ...but if you know, let me know and save me a trip to google π
I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
It's not really coercion, or I don't think it goes that far. It's just that !! is saying "make this a boolean if it's "truthy" and you can just use the "is it truthy" instead.
Casting to boolean is only useful if you're going to do something like supply it to an API that expects a boolean.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Your latter example would be the "implicit" coercion. Under the hood, I believe JavaScript is doing something similar to the
!!
way, right? So the result would be the same . I don't recall completely unfortunately ...but if you know, let me know and save me a trip to google πIt's not really coercion, or I don't think it goes that far. It's just that
!!
is saying "make this a boolean if it's "truthy" and you can just use the "is it truthy" instead.Casting to boolean is only useful if you're going to do something like supply it to an API that expects a boolean.