-1

I stumbled upon following code:

function $(id) {
    return document.getElementById(id);
}

Sadly, I don't know what it does. The only thing I found about this code was here: Make alias to document.getElementById in Javascript

aymericbeaumet
  • 5,464
  • 1
  • 32
  • 48

1 Answers1

3

$ is a valid identifier in JavaScript (read more here), you can use it for functions, variables and object properties. That being said, my experience makes me immediately think of jQuery, other developers might have this expectation too.

Back to your code: you define a function named $ returning a call to document.getElementById. There is no catch here, you can use it like any other JavaScript function:

const element = $('my-id');
aymericbeaumet
  • 5,464
  • 1
  • 32
  • 48
  • The reason people use `$` is because: 1. jQuery had it as universal selector function 2. it's smaller than writing `document.getElementById(id)` (you could save some bytes and keystrokes with this) 3. Chrome dev tools console allow you to select elements with this – Sid Vishnoi Apr 22 '18 at 17:29