Comments (10)
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.
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.
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.
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.
+1 to avoid !!.
!!""
being false is also error prone. An empty string is not the same as having no string at all.
from javascript.
Pruning this one. Feel free to reopen if you feel strongly about it.
from javascript.
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.
@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.
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.
@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)
- [spam] HOT 1
- [spam]
- [spam] HOT 1
- Multiline comments HOT 3
- [spam]
- [spam]
- TypeError: Failed to load plugin 'jsx-a11y' declared HOT 4
- [spam]
- [spam] HOT 1
- Regarding Updation HOT 4
- [spam] HOT 1
- [spam]
- Link 404 not found HOT 2
- Can we add on quiz sections on this repo too HOT 11
- [spam]
- eslint v9 support
- [spam]
- import/no-extraneous-dependencies shouldn't be a code style rule HOT 25
- Parsing error: Unexpected character '#' HOT 3
- [spam] HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript.