
Hidden Features of JavaScript - iamelgringo
http://stackoverflow.com/questions/61088/hidden-features-of-javascript
======
technoguyrob
I don't know why the second-top one has so many votes, it's completely wrong.

    
    
       var x = 1;
       var y = 3;
       var list = [0,1,2];
       x in list; //true
       y in list; //false
       1 in list; //true
       y in [3,4,5]; //true
    

That last expression is _false_. This is because the "in" operator does _not_
check for values in the list; it checks for keys. Try this:

    
    
       javascript:alert(3 in [3,4,5])
    

The only things that will be true for "X in [3,4,5]" are 0, 1, and 2 for X
(since these are the natural keys for the terms in the array). More
specifically, "in" in general checks for members associated with objects
(e.g., keys in an array, methods of a class, etc.):

    
    
       javascript:alert("The setFlag method " + ("setFlag" in {setFlag:function(){}} ? "exists" : "does not exist"))
    

EDIT: This makes the keyword considerably less useful (since indexOf doesn't
work for the Array object in IE--although jQuery has a way around this),
although it can be used to replace the less elegant

    
    
       if (typeof object.method == "undefined")

~~~
PieSquared
Oh whoa! I never realized I could just type "javascript:..." into the status
bar in Firefox and have it run it. That's awesome. (In retrospect, I realize I
should've realized it, because I've always known that the javascript links are
"javascript:functionCall()" or some such.)

------
axod
This is really scary. Hidden features??

Was this posted to show just how much misinformation and dumbing down there is
at stackoverflow?

As if we need more misinformation about javascript on the net.

~~~
jrockway
You know, it's kind of sad. Before reading the article, I was going to post a
reply to your comment like "but omg, javascript has functions that are
values!!1". Then I read the article, and found that that's the first "hidden
feature".

I guess "hidden feature" means "something that's not in PHP".

I also like how people use terms like "null coalescing operator" when everyone
else calls it "short circuiting". They are so smart!!11!

~~~
anthonyb
The "Hidden features of PHP" section is pretty damn funny actually, but not in
a good way. I can summarise it for you if you're lazy:

    
    
      "ZOMG - PHP has arrays! Arrays are like, super cool!"

------
wayne
If you want to learn something new about JavaScript, just skim through Douglas
Crockford's articles. As long as I've been doing JavaScript, I learn something
new or rediscover something every single time:
<http://javascript.crockford.com/>

------
river_styx
Wow, the top answer really doesn't understand first class functions at all.

~~~
bradgessler
Yeah, that's a pretty bad example!

~~~
ericwaller
That may not be a particularly interesting example, but I don't think it's
bad.

    
    
      var a = 5;
      var a = function() {
        alert('hello world');
      };
      var a = 2;
    

It's not a closure (which is what I assume you were hoping for), but it
definitely reveals something pretty unexpected about the language for someone
who has no experience with first class functions.

~~~
bradgessler
Yeah, that's also pretty bad. It doesn't show what you can do with the
function.

------
johns
I have enough rep on the site to edit any question or answer. If you find
something that's factually incorrect, post a comment on it, link it here and
I'll edit it. Get off your high horses and help some people out.

~~~
gruseom
High horse? I feel no impetus to participate in that community.

What I do find interesting is how it seems very much an extrapolation of Jeff
Atwood's blog - much as HN, I suppose, is largely populated by people who are
attracted to Paul Graham's essays. On the one hand, this is perfectly natural;
on the other hand I find it fascinating.

Edit: I'm not saying that the people in these communities necessarily agree
with the opinions of their founders.

------
kylec
I can understand covering the site itself, but posting links to the content?
Did we run out of real Hacker News?

~~~
iamelgringo
You could always try to find interesting content and post it:
<http://news.ycombinator.com/submitted?id=kylec>

------
andreyf
This is a matter of taste, of course, as we don't have a good study of
programming code comprehension yet, but I think the coolest part of JS are
cond statements using the trinary operator:

    
    
        var foo = predicate  ? "one" :
                  predicate2 ? "two" :
                               "default";
    

And short-circuit and/or:

    
    
        var foo = uncheckedFoo || "default";
        var wiz = (bar && bar(foo)) || "default";
    

Truthy values get in the way sometimes (especially 0), but it sure as hell
beats a ton of if...else crud.

Worst part of JS, while we're at it:

    
    
        true == "0" == 0 == false

~~~
boucher

         true == "0" == 0 == false 
    

This is untrue.

    
    
        > true == "0"
        false
        > "0" == 0
        true
        > 0 == false
        true
        > true == "0" == 0 == false
        false

~~~
DougBTX
I think he meant something like this:

    
    
        true == !0 == "0" == false
    

Which still isn't that interesting, since it is much the same as:

    
    
        true == (false == false)
    

More interesting, something like:

    
    
        > ! 0 ==   "0"
        false
        >   0 == ! "0"
        true
    

Since "!" acts as if false === 0.

