26. Jul 2021 |

How to declare jQuery variables

var $allParas = $("p"); – When a variable starts with a $ sign, like in the following line of code, it’s a convention that developers use to indicate when a variable is storing a jQuery collection. Which means that you don’t have to do it, but you can if you’d like.

Many developers like to prefix the names of variables that store jQuery collection objects with a $, because it makes it obvious that they can call jQuery methods on them. It’s particularly useful if your code has a mix of variables that store jQuery collections and variables that store DOM elements.

Also acceptable to use var pic = $("#picture"); variable which stores a jQuery collection object. That collection does indeed contain a reference to the selected DOM element object, but it also contains many other properties and methods.

Source: Khan Academy

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.