Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 556903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:58:09+00:00 2026-05-13T11:58:09+00:00

I always wondered why jQuery returns true if I’m trying to find elements by

  • 0

I always wondered why jQuery returns true if I’m trying to find elements by id selector that doesnt exist in the DOM structure.

Like this:

<div id="one">one</div>

<script>
    console.log( !!$('#one') ) // prints true
    console.log( !!$('#two') ) // is also true! (empty jQuery object)
    console.log( !!document.getElementById('two') ) // prints false
</script>

I know I can use !!$('#two').length since length === 0 if the object is empty, but it seems logical to me that a selector would return the element if found, otherwise null (like the native document.getElementById does).

F.ex, this logic can’t be done in jQuery:

var div = $('#two') || $('<div id="two"></div>');

Wouldnt it be more logical if the ID selector returned null if not found?

anyone?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:58:10+00:00Added an answer on May 13, 2026 at 11:58 am

    This behaviour was chosen because otherwise jQuery would regularly throw NullReference Exceptions

    Almost all jQuery functions return a jQuery object as a wrapper around the Dom elements in question, so you can use dot notation.

    $("#balloon").css({"color":"red"});
    

    Now imagine $("#balloon") returned null. That means that $("#balloon").css({"color":"red"});
    would throw an error, rather than silently doing nothing as you would expect.

    Hence, you just gotta use .length or .size().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One thing I've always wondered about is if the instances of std::string that I
I have always wondered that since Qt uses almost every C++ feature in the
I always wondered how exactly you'd handle caching of database-values (in C#, ASP.NET) so
I have always wondered how I should do this. Lets say I have a
Bit of a newb but just having a small problem using jquery cycle all
Possible Duplicates: How to write a crawler? Best methods to parse HTML I've always
Most of us know that to redirect STDERR to STDOUT we do 2>&1 We
I am currently working on a MVC3 project in C# and VS2010 using a
I usually try to make sure my object instances comply with the Liskov Substitution
UPDATE: Skip to the answer if you want to save yourself the lengthy preamble.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.