Giter Site home page Giter Site logo

Comments (10)

ssorallen avatar ssorallen commented on May 5, 2024

The example uses a Number, but the Boolean casting section is intended to be generic for any type. !!variable will cast any JavaScript type to a Boolean type.

What if age === 0 is a valid state? hasAge would be false in your example, but !!age would be true.

I agree checking values of Numbers should be done differently, but checking for truthiness is straightforward with !!.

from javascript.

 avatar commented on May 5, 2024

Actually you completely missed the point. First of all if age === 0 is a
valid state you would write age >= 0, not age === 0. The actual point is
there is no conversation between any type and bool. Types are not
convertible to truth. All types are truth. Only conditions are true or
false. And further the point is about readability and expressiveness and
maintainability. But elaborating on this any further won't help, if you
don't get it from my original post you can stick with your style, I only
wanted give you some of what I know.

On Fri, Nov 8, 2013 at 4:16 AM, Ross Allen [email protected] wrote:

The example uses a Number, but the Boolean casting section is intended to
be generic for any type. !!variable will cast any JavaScript type to a
Boolean type.

What if age === 0 is a valid state? hasAge would be false in your
example, but !!age would be true.

I agree checking values of Numbers should be done differently, but
checking for truthiness is straightforward with !!.


Reply to this email directly or view it on GitHubhttps://github.com//issues/97#issuecomment-28027076
.

from javascript.

ssorallen avatar ssorallen commented on May 5, 2024

The Boolean section is a possibly-too-abbreviated check for existence. What do you think of:

var hasAge = (age != null);

I can see how the double bang could be misinterpreted or possibly lead to the wrong value.

from javascript.

serbanghita avatar serbanghita commented on May 5, 2024

I agree with @bobef on " Only conditions are true or false. And further the point is about readability and expressiveness and maintainability."

Maybe the context is important, still var age = isNaN; !!age; is true and it gets confusing ... I prefer to avoid !! approach.

from javascript.

jaseemabid avatar jaseemabid commented on May 5, 2024

+1 to avoid !!.
!!"" being false is also error prone. An empty string is not the same as having no string at all.

from javascript.

goatslacker avatar goatslacker commented on May 5, 2024

Pruning this one. Feel free to reopen if you feel strongly about it.

from javascript.

ut3saturn avatar ut3saturn commented on May 5, 2024

Superb. Can you tell if casting is being used here? Or does this == operator have its vulnerabilities as well?

console.log(23 == "23")

Hard to tell if casting happens to the left side in order to compare it with the right. Would a true value occur?

from javascript.

ljharb avatar ljharb commented on May 5, 2024

@ut3saturn I’m not sure what you’re asking, exactly - all == operations do type conversions. As for which side is coerced, the rules are consistent but hard to understand, which is why it’s better to make the coercion explicit and use ===.

from javascript.

sstern6 avatar sstern6 commented on May 5, 2024

I was wondering what airbnb style guides thinks about the !!.

Is there a rule pref for/against:

const arr = [1,2,3];

if (!!arr.length) {
  // do something
}

To ensure the that the check for arr.length is a boolean value not a truthy or falsey value?

from javascript.

ljharb avatar ljharb commented on May 5, 2024

@sstern6 !!x is ideal to coerce to a boolean; but https://github.com/airbnb/javascript#comparison--shortcuts already discusses that you shouldn't rely on 0 being falsy; that for "length", you should always explicitly compare to a number.

from javascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.