0

I was following a tutorial for datamining from here.

I noticed that the variable $ was declared. What was the reason for a variable name like this?

var request = require("request"),
    cheerio = require("cheerio"),
    url = "http://www.wunderground.com/cgi-bin/findweather/getForecast?&query=" + 02888;

request(url, function (error, response, body) {
    if (!error) {
        var $ = cheerio.load(body),
            temperature = $("[data-variable='temperature'] .wx-value").html();

        console.log("It’s " + temperature + " degrees Fahrenheit.");
    } else {
        console.log("We’ve encountered an error: " + error);
    }
});
vasa.dhananjay
  • 165
  • 1
  • 11
  • I guess it's just a var name and has no impact what so ever (expect if another framework already use it), it could be named foo nothing would change – Stranded Kid Sep 19 '15 at 18:55
  • 2
    Maybe because `$` is short, visually very distinctive and easy to type? You can also use `Ω`, `ø` or `æ`, but do you know how to type them off the top of your head? – Felix Kling Sep 19 '15 at 18:59
  • also ties in well with using jQuery selector syntax – charlietfl Sep 19 '15 at 19:29

2 Answers2

0

$ is just a simple character, nothing special about it.

However, it's unlikely to be used as a 'normal' variable name, hence alot of libraries (notably jQuery) use it to to hold the library functions, and as a convention you know that it's a call to the library, not something that you've defined in your code.

There's no technical reason for this, but it makes it easier to recognize and type.

Note that this is similar to how Underscore.js and Highland.js use _ to represent their libraries.

renderf0x
  • 179
  • 6
0

The reason is that cheerio is a jquery alternative on the server and jquery uses the variable $.

tune2fs
  • 7,605
  • 5
  • 41
  • 57