aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes JavaScript style!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JavaScript style!!" Watch "JavaScript style!!" New topic
Author

JavaScript style!!

Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1739
    
    2
(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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

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)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39537
    
  27
I'm disappointed, I expected song, dance and music.


Ping & DNS - updated with new look and Ping home screen widget
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Here's a dancing ninja:

Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1739
    
    2
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaScript style!!
 
Similar Threads
help pls
EJB-QL
Boolean methods
java.lang.NullPointerException
What statement could you use in the WHERE clause to select all the rows in a table where no price is