Yehuda, I will agree and (respectfully) disagree with you. Yes, using the length property is expedient. It's how millions of programmers have been doing such things, in many different languages, and they will probably continue to do it. No harm there, as indeed JS treats zero as a false and it works fine.
However, I still maintain that semantically it is a hack. The question we are asking is "does this element exist?". Technically, that should be answered with a boolean result, and the .is() method's specific purpose is to check for the existence of selected elements and return a boolean response. Also, efficiency is not always the most important criteria in coding. Readability and maintainablility very often should be more important. If you have a loop checking for the existence of thousands of elements, then, yes, I'd say go for the faster solution. However, if you're just responding to a button click or something that only happens a few times (and especially if you or someone else will have to maintain it later), I'd recommend using the syntax that most clearly states the intent. The reason you need an entry in the FAQ about this, is that the old "length" hack that some of us have used for decades just isn't intuitive to newbies. I do enjoy arguing a trivial point long past answering the OP's question, but I draw the line at running speed tests. I have little doubt that .is() is not as fast as .length (heck, .is() probably uses .length internally), but unless you are doing huge amounts of processing, a millisecond or two of difference is not more important than good, legible coding style. I'm not saying don't use .length. It's short and it works fine for most cases. I'm just saying there is an alternative, too, to keep in mind, that is designed for the task, expresses the intent more clearly, and is more powerful. Larry On Dec 26, 10:58 am, Yehuda Katz <[EMAIL PROTECTED]> wrote: > I would definitely recommend $("#id").length. When working in a > language, it's important to keep in mind the language's idioms. In the > case of JS, 0 == false, so if($("#id").length) is a perfectly good > idiom. > > It's a bit confusing coming from another language (where 0 is true), > but that's not a good enough reason to reduce the efficiency of your > code. > > -- Yehuda > > On Dec 25, 2007, at 7:35 PM, McLars wrote: > > > > > > > $('#id').length is the old school, and most widely used, technique. > > This is probably the fastest. > > > $('#id').is('*') does make sense semantically (expresses the intent), > > and is more flexible. > > > Larry > > > On Dec 25, 12:33 am, "Alexey Blinov" <[EMAIL PROTECTED]> wrote: > >> Yep... my code have size(). Forgot to point it... > >> And thanks for info about more efficient way - using length. > >> So... which way is better than? > > >> 1. $('#id').length > 0 > > >> 2. $('#id').length() !== 0 > > >> 3. $('#id').is('*') //never try it... but look pretty > > >> - Alexey > > >> On Dec 25, 2007 5:39 AM, Michael Geary <[EMAIL PROTECTED]> wrote: > > >>> $('#id').size !== 0 would not work. I'll bet that your actual > >>> code has > >>> .size() instead of .size without the parentheses, right? The size > >>> property > >>> is a method, so the !== 0 test would always return true (since a > >>> function > >>> reference is never equal to 0). > > >>> .length is slightly more efficient than .size() - look at the > >>> source code > >>> for the .size method: > > >>> size: function() { > >>> return this.length; > >>> } > > >>> The only reason the size method exists at all is for compatibility > >>> with > >>> very old jQuery code that may have used it back in the days when > >>> there was > >>> no .length property. > > >>> -Mike- Hide quoted text - > > >> - Show quoted text -- Hide quoted text - > > - Show quoted text -