Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why sometimes in Javascript there is the dollar sign ($arg) before function argument?

Tags:

javascript

I see sometimes js snippets that are using the $ in front of the argument ($argName).

function someName(arg) {
  // some code
  // using arg
}

function someName($arg) {
  // some code
  // using $arg
}

In this js example it works either way with or without the $ in front of the arguments. Can anyone explaine if it has any use?

like image 591
Giorgio25b Avatar asked Sep 20 '25 17:09

Giorgio25b


2 Answers

The $ character is legal in JS identifiers, and is often used simply as a code convention to indicate that the passed parameter is already a jQuery object (as opposed to a native DOM element).

This serves as a reminder that it's not necessary to re-invoke the jQuery $(param) wrapper function on that parameter.

It's also used quite a lot in Angular JS code.

like image 77
Alnitak Avatar answered Sep 22 '25 06:09

Alnitak


It's sometimes used to reference an object from another library , like JQuery or AngularJS , what you're talking about here looks like AngularJs's dependency injection to me

UPDATE

See this answer it might be useful

like image 41
Yazan Rawashdeh Avatar answered Sep 22 '25 08:09

Yazan Rawashdeh