What does the $() function do in JavaScript?

That’s not part of ECMAScript (JavaScript). It’s just a function defined by some library of yours. Usually jQuery or PrototypeJS.