loading...

re: Use $ & $$ Instead of document.querySelector/All in JavaScript without jQuery VIEW POST

FULL DISCUSSION
 

If you work on a distributed team or with legacy code, and considering the potential for conflict (mostly with jQuery but some other libraries, and any code where someone may have used $ for document.getElementById(), as used to be common), I think it'd be a good idea to use trivially longer identifiers like this:

const $q = document.querySelector.bind(document);
const $qa = document.querySelectorAll.bind(document);

This is similar to a common method of using jQuery in noConflict mode, e.g.:

const $j = jQuery.noConflict();

It could be judiciously extended for similar methods if you use them often enough.

Code of Conduct Report abuse