You’re probably used to typical variables names such as the following:
var personName = 'Joe';
You may not realize it, but there are some non-alphanumeric variables at your disposal.
var $ = document.getElementById; var myElement = $('targetElement');
If you declare this variable outside of a function it will be a global variable and will compete with libraries that use the same global variable, so it’s probably best not to use it.
This standard specifies specific character additions: The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.
Some have come up with the solution of simply using two or more $$ symbols in order to distinguish the variable from libraries that just use a single $:
var $$ = document.getElementById; var myElement = $$('targetElement');
You will find that you can use the underscore _ in the same way to alias variables and functions:
var _ = document.getElementById; var myElement = _('targetElement');
If you’re really getting adventurous, you can even try using other symbols such as square root √, which seems to work just fine, just as $ and _ above. The only problem: it’s quite inconvenient using it, since it’s not available on any keyboards (except through some crazy key combinations perhaps).
Or you can put the symbol to use doing what you would naturally think it should do…
var √ = Math.sqrt; alert(√(4)); // 2