I am trying to learn jQuery and I came across this line in an example.
var $title = $(tag).attr('title');
Can someone please tell me what the prepended $ is for in $title.
The example seems to work fine if I replace $title with just title.
I understand this is probably a stupid question but it is a waste of time googling for “purpose of $”
Many thanks.
It doesn’t “mean” anything. The
$character is a legal character in Javascript variable names.However, there is a convention (which isn’t universal) of using
$as a prefix to any variable that points to a jQuery selection.You give the example:
This is a bad use of the
$character, according to this convention.$titleis a string, not a jQuery selection.This would be a correct use of the character:
Otherwise, a big reason for the prevalence of the
$character is that it is mandatory in PHP, and there is a big overlap between jQuery and PHP programmers.