+= (Addition Assignment) : Arithmetic operator : Operators JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Operators » Arithmetic operator »

 

+= (Addition Assignment)



















Syntax








variable += value














<html>
    <script language="JavaScript">
    <!--
    num = 42;
    str = new String("42");
    num += 8;
    str += 8;
    document.write("num = ",num,"<br>str = ",str);
    -->
    </script>
</html>



Additive operators also behave in special ways when dealing with special values. If the two operands are numbers, they perform an arithmetic add and return the result according to these rules:

If either number is NaN, the result is NaN.

If Infinity is added to Infinity, the result is Infinity.

If ?finity is added to ?finity, the result is ?finity.

If Infinity is added to ?finity, the result is NaN.

If +0 is added to +0, the result is +0.

If ?is added to +0, the result is +0.

If ?is added to ? the result is ?

If, however, one of the operands is a string, then the following rules are applied:

If both operands are strings, the second string is concatenated to the first.

If only one operand is a string, the other operand is converted to a string and the result is the concatenation of the two strings.





HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Operators
» Arithmetic operator