Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the dollar sign in Javascript, if not jQuery

I did some javascript / jQuery programming a few years ago, and I just started up again. Back then the dollar sign was used for all the jQuery functionality, and if no jQuery library was imported, the dollar sign was not defined.

Today, I started up Firefox, in a completely empty html file with no javascript libraries, and yet the dollar sign points to something. If I open the Firefox console and type '$' I get "function()".

1) Is it correct that dollar sign was not assigned, a few years back, or do I remember wrong?

2) What is the dollar sign, if not jQuery?

like image 941
Mads Skjern Avatar asked Mar 07 '14 08:03

Mads Skjern


People also ask

What does '$' 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 $() 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.

What is the dollar sign called in jQuery?

What does dollar sign ($) means in jQuery ? The $ sign is nothing but an identifier of jQuery() function. Instead of writing jQuery we simply write $ which is the same as jQuery() function. A $ with a selector specifies that it is a jQuery selector.

Can you use any other name in place of dollar sign in jQuery?

noConflict() "frees" the "$" from being associated with jQuery. Normally in your code you can use $ as a replacement for "jQuery". If you use noConflict() you can't do that anymore and so you have to replace each "$" with "jQuery"; .


3 Answers

1) Is it correct that dollar sign was not assigned, a few years back, or do I remember wrong?

That's correct and still true.

2) What is the dollar sign, if not jQuery?

Firefox and Chrome implement $, $$ and several others as helper commands. Both will set $$ to document.querySelectorAll(), and set $ to document.querySelector if window.$ hasn't been defined.

So what you're seeing isn't actually standard JavaScript, but a helper in the developer console of your browser. It's also not jQuery (as long as you're not on a page using jQuery). However, it's behaviour is close to the one of jQuery, concerning that querySelector (for single matches) and querySelectorAll (for multiple matches) give you almost the same strength as the jQuery selector.

like image 143
Zeta Avatar answered Oct 26 '22 01:10

Zeta


It means nothing to the interpreter just like the underscore

From the ECMAScript specification:

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code.

You may also check JavaScript Dollar Sign ($) - What is it for?

By convention, the dollar sign ($), the underscore (_) and even some ASCII character are permitted to be used anywhere in a JavaScript identifier (Source: Ecma Script documentation (7.6 Identifiers, ECMA-262, 3rd Ed.) the dollar sign is intended for use only in mechanically generated code. This means that we do not want to use the dollar sign ($) in our indentifier names, unless we are writing a framework. The following is a list of permitted characters that can be used anywhere in an identifier name:

IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart
IdentifierStart ::
UnicodeLetter
$
_
UnicodeEscapeSequence
IdentifierPart ::
IdentifierStart
UnicodeCombiningMark
UnicodeDigit
UnicodeConnectorPunctuation
UnicodeEscapeSequence

EDIT:-

Actually dollar sign function has become the more-or-less de facto shortcut to document.getElementById().

To confirm my point check this:

$(selector)

Returns a single element matching the given CSS selector. In old Firebug versions, this used to be equivalent to document.getElementById.

like image 27
Rahul Tripathi Avatar answered Oct 26 '22 00:10

Rahul Tripathi


Dollar sign($) was not assigned, but some browser add function for special usage.

Like Google Chrome, if you type $ on the console, it will return :

function $(selector, [startNode]) { [Command Line API] }

This function assigned for Google Chrome Developer Tool, and let debug more easier.

if you type $('div'), it will return something like this:

e.fn.e.init[178]

and include every div DOM object in it.

BTW, after you click the right button on the mouse to select element, you can acceess angular.js scope by type $scope on the console

like image 7
Chen-Tsu Lin Avatar answered Oct 25 '22 23:10

Chen-Tsu Lin