What does the minus equals below -=
mean/do?
$('#wrapper').animate({
backgroundPosition: '-=2px'
})();
Thank you
Subtraction (-) The subtraction operator ( - ) subtracts the two operands, producing their difference.
The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.
The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible.
== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. Checks the equality of two operands without considering their type. Compares equality of two operands with their types.
Adil has answered this but I always think it is useful to visualise problems and relate them to others.
The following two pieces of code have the same effect:
var a = 20;
a = a - 5;
and
var a = 20;
a -= 5;
In both cases a
now equals 15.
This is an assignment operator, what this means is that it applies whatever is on the right side of the operator to the variable on the left. See the following table for a list of assignment operators and their function:
Operator | Example | Same as | Result
______________________________________________
= | a = 20 | | a = 20
+= | a += 5 | a = a + 5 | a = 25
-= | a -= 5 | a = a - 5 | a = 15
*= | a *= 5 | a = a * 5 | a = 100
/= | a /= 5 | a = a / 5 | a = 4
%= | a %= 5 | a = a % 5 | a = 0
You also have the increment and decrement operators:
++
and --
where ++a
and --a
equals 21 and 19 respectively. You will often find these used to iterate for loops
.
Depending on the order you will do different things.
Used with postfix (a++
) notation it returns the number first then increments the variable:
var a = 20;
console.log(a++); // 20
console.log(a); // 21
Used with prefix (++a
) it increments the variable then returns it.
var a = 20;
console.log(++a); // 21
console.log(a); // 21
The operator -=
(Subtraction assignment) will subtract the given value from the already set value
of a variable.
For example:
var a = 2;
a -= 1;
//a is equal to 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With