What Is the Purpose of a Plus Symbol Before a Variable

What is the purpose of a plus symbol before a variable?

The + operator returns the numeric representation of the object. So in your particular case, it would appear to be predicating the if on whether or not d is a non-zero number.

Reference here. And, as pointed out in comments, here.

Can someone explain to me what the plus sign is before the variables?

+a // + is used to be sure that a is a number before adition
+ // is plus sign for adition!
+b // + here is used to be sur that b is number before adition

if we were sure that a and b are numbers (there where no need for that)

//you can replace + sign with
parseInt(a) + parseInt(b)
//or
parseFloat(a) + parseFloat(b)

as an examlpe imagine what hepens if you forget the + sign

"1" + 1 = "11"
//but
+"1" + 1 = 2

What does a plus sign do in front of a variable in Python?

What that plus sign does depends on what it's defined to do by the result of that expression (that object's __pos__() method is called). In this case, it's a Decimal object, and the unary plus is equivalent to calling the plus() method. Basically, it's used to apply the current context (precision, rounding, etc.) without changing the sign of the number. Look for a setcontext() or localcontext() call elsewhere to see what the context is. For more information, see here.

The unary plus is not used very often, so it's not surprising this usage is unfamiliar. I think the decimal module is the only standard module that uses it.

Why the plus sign in the expression logical expression `product.id === +id;`?

It converts a variable to a Number, if success: +id equals Number(id). If the conversion fails, it will return NaN

What is the + operator before a variable in Javascript?

It converts a String variable to a Number, if possible: +'21.2' equals Number(21.2). If the conversion fails, it return NaN (that's where || 0 kicks in in your example code)

JavaScript plus sign in front of function expression

It forces the parser to treat the part following the + as an expression. This is usually used for functions that are invoked immediately, e.g.:

+function() { console.log("Foo!"); }();

Without the + there, if the parser is in a state where it's expecting a statement (which can be an expression or several non-expression statements), the word function looks like the beginning of a function declaration rather than a function expression and so the () following it (the ones at the end of the line above) would be a syntax error (as would the absense of a name, in that example). With the +, it makes it a function expression, which means the name is optional and which results in a reference to the function, which can be invoked, so the parentheses are valid.

+ is just one of the options. It can also be -, !, ~, or just about any other unary operator. Alternately, you can use parentheses (this is more common, but neither more nor less correct syntactically):

(function() { console.log("Foo!"); })();
// or
(function() { console.log("Foo!"); }());

What's the meaning of $ in front of a variable

As everyone said, its just a convention.

I use the $ sign in front of a variable to identify that this variable holds an object.

var thisIsANumber = 1024; // No $.. Its a normal variable
var $divElement = $('div#idOfDiv'); // Yes! Its a jQuery Object
var $this = $(this); // Commonly used to reduce the work javascript has to do!

//Now I can use something like this.. (Notice how easy it is to read!)
$divElement.slideUp();

// Or a more `real world` example!
$('#element').click(function(){
// Hold $(this) inside a variable
// So we don't have to traverse the dom unnecessarily
var $this = $(this); // Save it (its a object.. so prepend a `$` )
$this.hide(); // Use it again
$this.fadeIn(); // and again
// ^ Has a dollar sign, because it is a jQuery Object.
});

You will see loads of plugins use this convention (well.. at least the well written ones).

By storing the object inside a variable, Javascript doesn't have to crawl through your code each time to get the element. Instead, we already have the element (inside the variable), so we use that to reference it.

If you use $(this) more then once inside the same callback function, you should store it inside a variable.. (var $this = $(this);). Otherwise, every time you use it, javascript will have to get the element from your source code each time (which can massively decrease performance! (especially for people browsing on a slow/old computer!).

What does the plus sign do in '+new Date'

That's the + unary operator. It's equivalent to:

function(){ return Number(new Date); }

See http://xkr.us/articles/javascript/unary-add and MDN.



Related Topics



Leave a reply



Submit