Giorgio25b
Giorgio25b

Reputation: 416

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

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?

Upvotes: 4

Views: 4146

Answers (2)

Alnitak
Alnitak

Reputation: 339786

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.

Upvotes: 8

Yazan Rawashdeh
Yazan Rawashdeh

Reputation: 1041

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

Upvotes: 1

Related Questions