Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've learned javascript at school but since I'm working with it and study about it every day, I've found very particular aspect of javascript that I didn't know about. Which at first, was very hard to understand for me and finally, I found it very usefull and easy to implement. And in the final, it gives to my code some kind of "beauty".

An example I've once seen:

function getter( input )
    result = {
        foo1 : 'bar1',
        foo2 : 'bar2',
        foo3 : 'bar3'
    }[input] || input || "default";

    return result;

Do you guys have other examples of particular use you make of Javascript ?

Thank you

PS: I use the term particular use because it might be unusual for any Javascript beginner. I believe this question is most likely to belong to the community wiki.


migration rejected from Oct 28 '15 at 21:38

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as too broad by MichaelT, GlenH7, durron597, Dan Pichelman, gnat Oct 28 '15 at 21:38

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

Since this doesn't have an objective answer, it should probably be a Community Wiki. – user12157 Feb 2 '11 at 15:20
@patrick dw, I totally agree but I can't convert/create a community wiki post. – Cybrix Feb 2 '11 at 15:22
I see. I didn't realize that more rep was needed to make a CW. There's no CW checkbox under the question's edit area? EDIT: Just looked at meta, and sure enough, only moderators can do it now. – user12157 Feb 2 '11 at 15:25
@patrick dw, for some reason only 10 reputations is required to create wiki post. But the checkbox wasn't there and in fact, I've not seen it for a while. (When I created my account before I remember that checkbox). – Cybrix Feb 2 '11 at 15:31
@Cybrix: Yeah, there are some posts on meta indicating that now only moderators can do it. – user12157 Feb 2 '11 at 15:33


jQuery makes such extensive use of closures and wrapping objects that it pretty much turns JavaScript into a completely different language.


$('ul.level-2').children().css('background-color', 'red');


jQuery is interesting in that often it's completely ambiguous whether you have a single element or a list of elements, and as a result most (all?) of the same functions can be run in either case.

actually in jQuery you always have a list, it just may be a list of 1 element. – Zachary K Feb 16 '11 at 13:46

I tend to do a lot of list operations and recursion. Almost treating javascript as if it was lisp.

somthing like this

   function getNegative (elements){ 
      return elements.filter(function (el){  return el< 0; });

very clean and easy to understand

Yes indeed it's very clean. – Cybrix Feb 16 '11 at 14:09
actually I will often stack array ops, put 3-5 of them in a row, each one is generally a function of 1-3 lines of code. – Zachary K Feb 16 '11 at 14:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.