Search code examples
javascriptjqueryoperatorscomparison-operators

Why does `$(document) === $(document)` returns false in jQuery?


I tried $(document) === $(document) but found the result is false..

Does anyone have ideas about this?


Solution

  • When you use jQuery you get a JS object back. This object is totally different every time you use the jQuery selector.

    To understand this better, I played in the console with some arrays:

    a = [1, 2]
    [1,2]
    b = [1, 2]
    [1,2]
    a == b
    false
    a === b
    false
    

    When using jQuery it's just like using objects, because you don't get a DOM element in response (maybe that why you got confused)

    How Can You Do It?

    If you do want to compare 2 jQuery objects you could use the is() jQuery method:

    $(document).is($(document))
    true