DEV Community

Discussion on: Use $ & $$ Instead of document.querySelector/All in JavaScript without jQuery

Collapse
 
denmch profile image
Den McHenry

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);
Enter fullscreen mode Exit fullscreen mode

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

const $j = jQuery.noConflict();
Enter fullscreen mode Exit fullscreen mode

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