Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery Variables or When should I use a $

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #28983
    BrettZ
    Member

    I am just getting into jQuery and I know absolutely nothing about javascripting. The one thing I do not understand is the use of the "$". For example how is

    var newHeight = $("#page-wrap").height();

    different from

    var $newHeight = $("#page-wrap").height();

    likewise, with the plugin template http://css-tricks.com/snippets/jquery/jquery-plugin-template/, what is the difference between

    base.$el = $(el);
    and
    base.el = el;

    Thanks
    BZ

    #75380
    noahgelman
    Participant

    The $ is purely optional and doesn’t affect the rest of the code. So why is it sometimes used to ask? Because it helps you to understand what you’re targeting when you use it later. If it’s targeting a DOM property, you don’t use it. When you’re targeting a jquery property, you do. It’s just better for visualization when you’re looking at the code as well as during the coding process. However, feel free to call it whatever you feel comfortable with.

    #75394
    Rob MacKay
    Participant

    Yea vars don’t need it in jQuery but it is a good way to show they are vars compared to other things… some people like to use other chars like "_" for example.

    #75428
    BrettZ
    Member

    Thanks, guys. Your answers have been very helpful. That clears up most of my confusion about jQuery. The screencast mentioned above was also a great source of basic syntax and application. I would also recommend the 15 part tutorial available from themeforest.

    BZ

    #75433
    Chris Coyier
    Keymaster

    The reason I do that on some variable and not others is to designate which ones represent jQuery objects and which ones don’t.

    Code:
    var $pagewrap = $(“#page-wrap”);

    var height = 50;
    var width = $(“#thing).width();

    It’s totally optional and has no real meaning, just a "convention" I like.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.