views:

1027

answers:

17

(I understand that someone else asked a similar question and it was closed as 'argumentative', but I'm really interested in understanding the arguments around this.)

I know JavaScript really well. I've been writing it professionally for years. I've internalized a lot of the cross-browser incompatibilities and sketchiness, know DOM manipulation like the back of my hand, have worked with some of the best web developers in the industry & picked up a lot of their mojo.

I've been checking out jQuery. I understand the point of a javascript library (how many times have I written animation, getElementsByClass, and hide/show functions?). But to be honest, it seems like a waste of time to learn an entirely new syntax that isn't less complex. It seems like I'd be bashing my head against a wall to learn an entirely new interface to the same old JavaScript.

I'm not technically an engineer, so maybe I'm missing something. Could someone spell out the tradeoffs of jQuery? Is it really faster to learn and understand jQuery syntax than to just learn JavaScript?

+1  A: 

jQuery allows you to write shorter, cleaner code that's easy to understand. Many people use it, so there are many plugins that work along with it, minimizing bloat. The same may not be true of your own personal library.

Jamie
+1  A: 

If you know JavaScript really well you should also know that jQuery's syntax isn't different from JavaScript's syntax.

What you might be referring to is the chaining idiom (used everywhere in jQuery) which seems to make things look very different from other sort of code. Well there's a reason for that, it's because it bonds really well with how the DOM works.

Luca Matteis
I think chaining is designed to make coders feel clever about writing one-liners instead of writing readable, debuggable code. I see no advantage in chaining over multiple lines of code, and I've yet to meet anyone who could convince me otherwise. But I do like the rest of jQuery.
Daniel Lew
Agree with @Daniel here.
Rex M
There is a difference in download size. It might be trivial, but that's one advantage to chaining.
VirtuosiMedia
If you're worried about download size, get a JavaScript minimizer; there are plenty out there.
Daniel Lew
+3  A: 

Speed of development and turnaround of code.

Keith Donegan
+11  A: 

I'm not technically an engineer, so maybe I'm missing something. Could someone spell out the tradeoffs of jQuery? Is it really faster to learn and understand jQuery syntax than to just learn JavaScript?

jQuery is more than "just another interface" to Javascript. It allows you to express yourself in ways that are more compact and succincter than the corresponding Javascript implementation. At the same time, it's clearer and much more powerful. Specifically, the benefits you get include:

  • Expressiveness. jQuery is essentially a DSL for DOM manipulation and querying. This specificity is a major source of its utility and effectiveness.

  • Cross-browser. To a very large extent, jQuery is "write once, run anywhere". Even in 2009, this is still a surprisingly rare feat for web-based platforms. It's gratifying and relieving to know that you won't have to waste time debugging obscure problems on IE6 (well, most of the time).

  • Highly complete documentation. As a developer, I prize APIs and frameworks that have taken the time to spell out what all the moving pieces are supposed to be doing. Nothing is more encouraging than knowing that (1) things are stable enough that writing documentation isn't an attempt to hit a moving target and (2) things are useful enough that they've attracted enough people to flesh out the documentation fully.

In sum, the difference between Javascript and jQuery is analogous to the difference between assembly and higher-order programming languages. Although they're both technically both "an interface to machine language" and can both do similar things, the latter is considered far more powerful because of how easy it is to represent things and develop rapidly.

John Feminella
+4  A: 

You can't learn jQuery without learning JavaScript, and you can't be a jQuery guru without being a JavaScript guru.

That said, it really is much faster to do things with jQuery than with "bare metal" JavaScript. Moreover, the way one works with jQuery is at a far more abstract level than the way one works with "bare metal" JavaScript. In addition, the jQuery syntax is very basic and not at all hard to learn, although the way you think about jQuery is very different from the way you think about "bare metal" JavaScript but enables you do to much more, much more rapidly.

Justice
+4  A: 

community support

other developers writting and testing code, make it possible for you to do things you simply do not have time to do. Its not about doing anything you do not know how to do, its about doing things quickly, efficiently, and of very high quality (its already been tested).

Rob Fuller
+23  A: 

There are a few big benefits to using a framework over homegrown/handwritten code:

  • Abstractions. I'm sure you're very proud of the fact that you've slung enough JS to be able to write animations from scratch. You should be! However, abstracting common functionality away from yourself is actually very liberating. You just call the method and know the innards will be executed, and executed well. Even if you're very fast at writing the low-level stuff yourself, that's still time you've spent on that instead of solving today's problems.

  • Common language. Using a common framework is like speaking a common language. You can collaborate with other developers very easily, and each can pick up where others left off without friction. (Compared to stepping into an application which uses a homegrown library for the things jQuery can do.)

  • Experts. The people working on jQuery are JavaScript gods. I am really, really good at JavaScript, and you probably are too, but there's a big difference between normal good and jQuery good. A team of insanely good people are constantly poring over a small set of common functionality - tuning it, tweaking it, enhancing it to make it the best it can possibly be. That represents a huge number of man-hours a single person like you or me simply cannot reproduce, no matter how good we are. And if you are as good as the jQuery guys, you can only benefit by combining your talent with theirs and contributing to the jQuery codebase. It's a melting pot of insane talent.

Rex M
I'm not sure I agree with the "common language" part. JavaScript can just as easily be a common language as jQuery (and at the moment is probably moreso, though jQuery is certainly picking up in popularity).
Daniel Lew
@Daniel yes, but the question is asking why use jQuery instead of manually implementing the things jQuery does. The "common language" I am referring to is stepping into an app that uses jQuery vs stepping into one that uses something homegrown.
Rex M
Ah, so you mean a common language for the abstraction you were talking about in the first point. I agree with that.
Daniel Lew
@Daniel clarified in the answer, thanks!
Rex M
Whoa, your last point there is a little over the top. Have you *looked* at the latest jQuery code? Some of it is downright crap. Here's a polarized opinion on the matter: http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.javascript/2009-02/msg00278.html
JPot
Note: still a great DOM library, without question.
JPot
Somewhat humorous answer that deflates jQuery as the "pinnacle of Best Practice" (near bottom): http://stackoverflow.com/questions/676249/deep-cloning-vs-setting-of-innerhtml-whats-faster/#678528
JPot
@jpot jQuery is not perfect, but just because some parts of it aren't very good doesn't mean it's not amazing as a whole. it's extremely effective and powerful, and beats most other libraries on most counts.
Rex M
+4  A: 

From the sounds of it, you know much of the same stuff that jQuery knows about the DOM, and I can only assume you've built-up a nice DOM toolset for yourself over the years that incorporates all of this knowledge. In short, No, you don't need to use jQuery. However, some of the rest of us do not know as much about the DOM, and jQuery levels the playing field so we can get on with getting work done for our clients.

That is not to say you should not learn jQuery (as opposed to using it). You may pickup a few things you didn't know. The 3 main features that distinguish it from other DOM libraries (including your own probably) are:

  1. built-in support for CSS selector syntax, useful for finding elements
  2. methods that operate on the set of wrapped elements
  3. chaining. Every method in jQuery.prototype (with "setter" behaviour) returns this

Personally, 1) and 3) don't hold a high premium. But 2) turns out to be huge for me. I never realized until jQuery how much my code tended towards operating on a set of nodes rather than a single node (if someone would have asked me I would have guessed the opposite tendency). 2) virtually eliminated all for loops from my code (or .forEach or however else I tended to abstract it), which was actually quite liberating. Now I notice it's the rare occasion where my code must operate on some single element.

JPot
Hey, you convinced me if not Ellen B :)
SirDemon
+4  A: 

One thing I don't see mentioned is that the library is written to work cross browser on a wide range of popular browsers and platforms: IE6+, Firefox 2+, Safari 3+

That alone is reason enough to use jQuery then to write your own JavaScript and have to worry about cross browser issues yourself.

RedWolves
100% agree - that's why I use it even for the most trivial jobs now :)
Paul Suart
+1  A: 
geetee
A: 

Jquery is javaScript in the hand of the designer.

It is as easy as writing CSS styles on an element.

Also Jquery is the easiest to understand, write maintain, add plugins.

adardesign
+3  A: 

One thing I haven't seen mentioned in the other answers is that, eventually, someone else is going to have to maintain your code. If it's all custom, they'll have a rougher time of it than if a more standard library is used. If that's not of any concern to you, and you don't think you're likely to need any of the really slick jQuery modules, then keep on with what you're doing.

BonkaBonka
+3  A: 

For me, there are several benefits, like speed of development, etc.

But at the end of the day, it boils down to one thing: It removes a HUGE amount of the cross-browser BS that can eat up so much time and resources, so I don't have to deal with it

Eli
+1  A: 

I was working on a gallery program for a client (which is now exhibiting inexplicable behavior in IE6 and 7 - surprise, surprise - but I found that when I switched from bare-metal Javascript to jQuery a lot of the work got much easier. To me, it makes available the CSS view of the DOM while writing Javascript - which makes traversal and manipulation much more intuitive. Also, the cross-browser compatibility and brevity is wonderful.

ehdv
+1  A: 

It doesn't really matter if jQuery is great or awful. It's the powerhouse JS library now, used on a huge number of pages, and future browsers will have to accommodate it or be seen as themselves buggy.


What I don't like about jQuery: I'm not that thrilled about how it ignores mobile phone browsers in its test suites. It gets pulled in on all kinds of pages on mobile phones, and yet it makes no effort to ensure it works well on them.

Nosredna
A: 

Because jQuery is awesome

Orion Edwards
A: 

jQuery degrades gracefully and it's one of the fastest (not THE fastest anymore though):

http://mootools.net/slickspeed/

There are many other frameworks out there, and whichever you choose, you probably wouldn't go wrong, but having uses jQuery myself, I'd definitely recommend it.

:)

Joe Zephyr
This test is very old, plus selector speed != not over all speed.
Jourkey