Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Timothy Wilson
Timothy Wilson
10,955 Points

When to use jQuery?

Maybe this is a silly question but in what situation(s) would you use jQuery and in what situation(s) would you write out pure JavaScript? I know that it all depends on what you want to do with your site but is it conceivable that you would only have jQuery code on your site or would you generally use a mixture?

1 Answer

Nicholas Olsen
seal-mask
.a{fill-rule:evenodd;}techdegree
Nicholas Olsen
Front End Web Development Techdegree Student 19,342 Points

If a web page only requires very simple functionality (like form validation) then you might consider writing pure javascript. It will make your web pages more efficient since it doesn't have to download a giant jQuery file. The caveat to this is that browsers very often do not interpret javascript the same, or their DOMs may be implemented differently. This is less of an issue in recent versions of major browsers, but if you're writing scripts for older browsers then it is probably advisable that you just use jQuery. (This is really the big reason jQuery was created to begin with).

jQuery solves cross-browser issues, has lots of nice plugins you can use and just has a better interface than native javascript and the DOM. However, if you're using jQuery for one small feature of your website, then you're effectively bringing in a bunch of code you're not using. If your webpage doesn't use ajax, then you're not going to use the $.ajax() method. But it is still there and it is taking up space causing your web page to load just a little more slowly. The same goes for ALL of the other jQuery code you're not using.

I think it just really boils down to your goals. If you just want to create a quick and easy project, support older browsers or have some really heavy javascript based effects then I think your best bet is to use jQuery. If you decide that you are going to use jQuery for anything, then you should probably use jQuery for everything. I wouldn't import jQuery for animations and then use vanilla javascript for form validations.

If you're worried a lot about efficiency I would consider using pure javascript (perhaps look into some javascript micro-libraries.) If you do decide to write in pure javascript, then you should do your research on writing cross-browser javascript and have a good testing strategy for testing your javascript in each browser.

If all of this seems super overwhelming, then just stick jQuery. :D

Timothy Wilson
Timothy Wilson
10,955 Points

Thank you so much. That really did help me.

James Barnett
James Barnett
39,199 Points

. If you decide that you are going to use jQuery for anything, then you should probably use jQuery for everything. I wouldn't import jQuery for animations and then use vanilla javascript for form validations.

That's great advice :+1: