There are some JavaScript libraries that use the $ (like Mootools). What if you want to use both libraries but don’t want the drama of getting your dollar signs muddle up? Sure you can use noConflict, but that’s no fun, right?

Use this pattern and you’ll be fine:


By using the reserved word jQuery (which substitues for $), and passing the $ in the main parenthesis, any code within the current block will use the $ symbol as part of jQuery and not any other library. A thing of beauty.

UPDATE: I just learned something new…

For a more efficiant way of keeping the scope chain, you can alternatively use the following:


The difference here is that instead of starting the jQuery scope on the first line, you can pass it as an argument to itself. This keeps things a bit more tidy.


I’d like to clear something up. The previous method (above) is a self invoking method, it isn’t part of jQuery, nor is it an actual a ready method. It get’s called immediately, but not necessarily when the DOM is ready. Technically, the appropriate thing to do is this:


This way you can take advantage of jQuery ready() method AND keep your $ within scope. Awesome? Yes, it is.