This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

JavaScript style!!

 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) What are folks thoughts on using the double NOT operator in JavaScript (or in any other languages where it's useful) to convert falsy values to false? ("True" false.) :P
(2) Do you expect all JavaScript programmers to be familiar with this idiom?

For example, variable "a" could likely be true, false, or null. Would something like this,

return !!a;

whose intent is to return either true or false (by "converting" null to false), cause conniptions for you or your team?

(3) Does your answer depend on the likely specific falsy value(s) you're converting from? For example, it's OK for null -> false, but not for other things like empty string, zero, undefined, or NaN?
 
Marshal
Posts: 67340
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) No problem.
2) No. But they should.
3) No, depends on context (does it make sense where it is being used)

(P.S. I gave you a cow not only for an interesting topic, but for your clever subject)
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm disappointed, I expected song, dance and music.
 
Bear Bibeault
Marshal
Posts: 67340
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a dancing ninja:

 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:(P.S. I gave you a cow not only for an interesting topic, but for your clever subject)



Mooo. (Thanks.)

Bear Bibeault wrote:3) No, depends on context (does it make sense where it is being used)



I've used !! for such purposes twice in the past week, and in both cases I think it makes the code clearer. I just needed to step back for a minute and make sure I'm not in toddler-with-a-hammer mode.

I'll mention my code at our next dev team meeting and will be prepared to fight off charges of "excessive cleverness" or the like.

While I was thinking about this kind of stuff yesterday, I went poking around jQuery source and found the following:



So at first glance I thought this was pretty clever stuff (and possibly an argument in my favor regarding the use of !!): the "getter" function fired always returns true or false, despite the underlying value only ever being undefined or true (in the current code base).

But then I started to wonder, in this case, why not just initialize fired to false when declaring it and avoid the !!. The only code I see testing the value of fired tests it as "!fired" -- which would return the same value (true) whether fired is undefined or false. Is there something going on here I'm not understanding, or should I just chalk it all up to a style preference?
 
Bear Bibeault
Marshal
Posts: 67340
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it just comes down to a style pref.

I see JavaScript written by people coming from Java that is stylistically different from those coming from a different background (jQuery committers tend to be from the Ruby rather than Java world).

A Java person might use the initialized variable approach; a non-Java person might not. Neither is wrong, but double-bang is a common idiom that all JavaScript authors should become familiar with.
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!