Is there an "exists" function for jQuery?

javascript, jquery

Solution

In JavaScript, everything is 'truthy' or 'falsy', and for numbers `0` means `false`, everything else `true`. So you could write:

if ($(selector).length)

You don't need that `>0` part.

Problem

How can I check the existence of an element in jQuery? The current code that I have is this: ``` if ($(selector).length > 0) { // Do something } ``` Is there a more elegant way to approach this? Perhaps a plugin or a function?

Original source