Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript/jQuery variable, what is the proper way to use a var?
    primarykey
    data
    text
    <p>I am sorry if this is a dumb or easy question but I am fairly new to Javascript/jQuery. The past month I have really started to delve into the land of scripting and have seen two different, maybe three, ways people use <code>var</code> in Javascript/jQuery.</p> <p>The way I use a var is like so,</p> <pre><code>var nav = $('nav'); nav.hide(); </code></pre> <p>A very common way I have seen people use vars,</p> <pre><code>var nav = $('nav'); $(nav).hide(); </code></pre> <p>From the answers,</p> <pre><code>var $nav = $('nav'); $nav.hide(); </code></pre> <p>From what I have learned from searching through Google is what you typed inside the variable is saved there to later be used. I then figured if I wrote the $() around the var when I was using it, it would duplicate the $(). Yet both ways seem to work so I know it does not duplicate it and therefore can tell that it is the same thing.</p> <p>Is there a correct way to use <code>vars</code> or are both equally the same and it doesn't matter?</p> <p>I apologize again if this is a known answer and will be happy to remove it if someone can show me the original question but I couldn't find anything on it.</p> <p><em>A great bit of information from an answer that I didn't mark as the answer but I find to be very important.</em></p> <pre><code>var element = document.createElement("div"); $("body").append(element); $(element).hide(); </code></pre> <blockquote> <p>In the example above, $(element) is necessary, because it takes a DOM object and converts it to a jQuery selector. jQuery functions only work on jQuery selectors, so you couldn't simply do element.hide().</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload