Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain the dollar sign in Javascript?

The code in question is here:

var $item = $(this).parent().parent().find('input'); 

What is the purpose of the dollar sign in the variable name, why not just exclude it?

like image 208
Keith Donegan Avatar asked May 11 '09 03:05

Keith Donegan


People also ask

What do dollar signs mean in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

What does dollar sign mean in coding?

When used in a regular expression, the dollar sign is used to represent the end of the line or string. For example, in the following Perl code, if the user's input stored in the $input variable ends with the "example," it would print "I see example." to the screen.

What does $() mean in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.

What does the $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.


2 Answers

A '$' in a variable means nothing special to the interpreter, much like an underscore.

From what I've seen, many people using jQuery (which is what your example code looks like to me) tend to prefix variables that contain a jQuery object with a $ so that they are easily identified and not mixed up with, say, integers.

The dollar sign function $() in jQuery is a library function that is frequently used, so a short name is desirable.

like image 64
cobbal Avatar answered Sep 16 '22 20:09

cobbal


In your example the $ has no special significance other than being a character of the name.

However, in ECMAScript 6 (ES6) the $ may represent a Template Literal

var user = 'Bob' console.log(`We love ${user}.`); //Note backticks // We love Bob. 
like image 44
UpTheCreek Avatar answered Sep 19 '22 20:09

UpTheCreek