explaining the differences between querySelectorAll and $$

correct me if i'm wrong, the double dollar sign ($$) is a shorthand for (querySelectorAll) in JavaScript.
but what i've notice in this code:

document.querySelectorAll('#main')

NodeList[1]
0:section#main.group
length:1
__proto__:NodeList

that it return a nodelist
and when i use:

 $$('#main')
Array[1]
0:section#main.group
length:1
__proto__:Array[0]

it return an array like obj
so they aren't the same, right ?!!


One thing i'm not using jQuery so is the dollar sign originally part of javascript and jQuery just using it or its different ?!

Answers 1

  • As you can see in the below snippet, $$ is undefined.

    It is a Command Line code by browser which works only when typed in console where document.querySelectorAll works in both the script in page as well as console.

    If you type

    $$;
    

    in console, it will return like this

    function $$(selector, [startNode]) { [Command Line API] }

    But if you type this,

    document.querySelectorAll;
    

    It will return

    function querySelectorAll() { [native code] }

    $$ is a function only for testing purpose like developer tools.

    console.log(document.querySelectorAll('div'));
    console.log($$('div'));
    <body>
    <div>div1</div>
    <div>div2</div>
    <div>div3</div>
    </body>


Related Articles