// Testing whether a selection contains elements.
if ( $( "div.foo" ).length ) {
    ...
}