What is a dollar sign in javascript if not jQuery

A few years ago, I developed several javascript / jQuery programs, and I just started again. Then the dollar sign was used for all jQuery functions, and if the jQuery library was not imported, the dollar sign was not defined.

Today I ran Firefox into a completely empty html file without javascript libraries, and yet the dollar sign points to something. If I open the Firefox console and type '$' , I get "function()" .

1) Is it right that the dollar sign was not assigned several years ago, or do I remember incorrectly?

2) What is a dollar sign if not jQuery?

+65
javascript jquery
Mar 07 '14 at 8:12
source share
6 answers

1) Is it right that the dollar sign was not assigned several years ago, or do I remember incorrectly?

This is true and true.

2) What is a dollar sign if not jQuery?

Firefox and Chrome implement $ , $$ and several others as helper commands. Both set $$ to document.querySelectorAll() and set $ to document.querySelector if window.$ not defined.

What you see is actually not standard JavaScript, but an assistant in your browser’s developer console. It is also not jQuery (until you are on the page using jQuery). However, this behavior is close to that of jQuery that querySelector (for single matches) and querySelectorAll (for multiple matches) gives you almost the same strength as the jQuery selector.

+87
Mar 07 '14 at 8:18
source share

It doesn't mean anything to the interpreter, like underlining

From the ECMAScript specification:

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

You can also check the Dollar Sign JavaScript ($) - for what?

By convention, the dollar sign ($), underscore (_), and even some ASCII characters are allowed to use the JavaScript identifier anywhere (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 identifier names unless we write a framework. The following is a list of allowed characters that can be used everywhere in the identifier name:

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

EDIT: -

In fact, the dollar sign function has become more or less de facto a shortcut to document.getElementById() .

To confirm my checkpoint:

$ (selector)

Returns one element matching the given CSS selector. In older Firebugs , this was equivalent to document.getElementById .

+12
Mar 07 '14 at 8:13
source share

Dollar sign($) not assigned, but some function of adding a browser for special use.

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

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

This feature is assigned to the Google Chrome Developer Tool and makes it easier to debug.

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

 e.fn.e.init[178] 

and include every DOM div object in it.

BTW, after you right-click to select an item, you can invite the angular.js region as $scope in the console

+5
Mar 07 '14 at 8:38
source share

Note that $$ not exactly document.querySelectorAll , because unlike this function, it does not return a NodeList :

 document.body.querySelectorAll('p') instanceof NodeList true $$('p') instanceof NodeList false Array.isArray($$('p')) true 

So $$(selector) really more like Array.from(document.querySelectorAll(selector)) . This means that .forEach type array methods and friends are available on $$ , which is actually quite useful.

+5
Dec 6 '15 at 21:12
source share

It could be anything, since $ is a valid variable name, like dollar .

From ECMAScript :

 Identifier :: IdentifierName but not ReservedWord IdentifierName :: IdentifierStart IdentifierName IdentifierPart IdentifierStart :: UnicodeLetter $ _ \ UnicodeEscapeSequence 

The simplest solution to see what it is and where it is defined would probably be to type $() and set a breakpoint on this line.

+1
Mar 07 '14 at 8:13
source share

To fulfill other answers here, MooTools also uses $ as an alias for document.getElementById .

It checks to see if $ is executed and by default will be document.id .

0
Jan 28 '16 at 18:45
source share



All Articles