JavaScript Tutorial/Operators/Arithmetic operator

Материал из Web эксперт
Перейти к: навигация, поиск

+= (Addition Assignment)

Syntax



   <source lang="javascript">

variable += value</source>


+ (Addition) with data type conversion

   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>
   </html></source>
   
  

Add string and integer together

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


Append two strings together

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


Arithmetic operator in action

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


Arithmetic Operators (+)

All the common arithmetic operators will attempt to convert strings to numbers when applicable.

If a string cannot be converted to a number, NaN (Not A Number) will be returned.

Addition (+)

If the values on either side are numerical values, the values are added together.

IF the values are strings, they are concatenated together.

The following line of code



   <source lang="javascript">

var resultOfAdd = 34 + 12;</source>


Compound assignment operators

Compound assignment operators exist for each of the major mathematical operations and a few others as well:

Multiply/Assign (*=)

Divide/Assign (/=)

Modulus/Assign (%=)

Add/Assign (+=)

Subtract/Assign (-=)

Left Shift/Assign (<<=)

Signed Right Shift/Assign (>>=)

Unsigned Right Shift/Assign (>>>=)

2. 1. Arithmetic operator 2. 1. 1. Compound assignment operators 2. 1. 2. <A href="/Tutorial/JavaScript/0040__Operators/ArithmeticOperators.htm">Arithmetic Operators (+)</a> 2. 1. 3. <A href="/Tutorial/JavaScript/0040__Operators/Appendtwostringstogether.htm">Append two strings together</a> 2. 1. 4. <A href="/Tutorial/JavaScript/0040__Operators/Addstringandintegertogether.htm">Add string and integer together</a> 2. 1. 5. <A href="/Tutorial/JavaScript/0040__Operators/Additionwithdatatypeconversion.htm">+ (Addition) with data type conversion</a> 2. 1. 6. <A href="/Tutorial/JavaScript/0040__Operators/AdditionAssignment.htm">+= (Addition Assignment)</a> 2. 1. 7. <A href="/Tutorial/JavaScript/0040__Operators/Subtraction.htm">Subtraction (-)</a> 2. 1. 8. <A href="/Tutorial/JavaScript/0040__Operators/Multiplication.htm">Multiplication (*)</a> 2. 1. 9. <A href="/Tutorial/JavaScript/0040__Operators/MultiplicationAssignment.htm">*= (Multiplication Assignment)</a> 2. 1. 10. <A href="/Tutorial/JavaScript/0040__Operators/Division.htm">Division (/)</a> 2. 1. 11. <A href="/Tutorial/JavaScript/0040__Operators/WorkingWithJavaScriptDivideOperators.htm">Working With JavaScript Divide Operators</a> 2. 1. 12. <A href="/Tutorial/JavaScript/0040__Operators/DivisionAssignment.htm">/= (Division Assignment)</a> 2. 1. 13. <A href="/Tutorial/JavaScript/0040__Operators/Arithmeticoperatorinaction.htm">Arithmetic operator in action</a> 2. 1. 14. <A href="/Tutorial/JavaScript/0040__Operators/UnaryNegation.htm">Unary Negation</a> 2. 1. 15. <A href="/Tutorial/JavaScript/0040__Operators/Stringandadditionoperator.htm">String and addition operator (+)</a>

Division (/)

If either of the operands is a string, an attempt is made to convert the string to a number.

For example, the following line of code



   <source lang="javascript">

var resultOfDiv = 42 / 7;</source>


/= (Division Assignment)

Syntax variable /= value



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


Multiplication (*)

The multiplication operator (*) multiplies the left operand by the right operand.

When either of the operands are strings, an attempt is made to convert the strings to numbers.

For example, the following line of code



   <source lang="javascript">

var resultOfMult = 5 * 7;</source>


*= (Multiplication Assignment)

   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


String and addition operator (+)

If the values on either side of the addition operator are strings, the strings are concatenated together.

If only one of the values is a string, the other value is converted to a string and concatenated with the first value.



   <source lang="javascript">

<html> <SCRIPT LANGUAGE="JavaScript">

</SCRIPT> </html></source>


Subtraction (-)

The subtraction operator (-) subtracts the number to the right of the operator from the number on the left.

When either of the operands are strings, an attempt is made to convert the strings to numbers.

For example, the line of code:



   <source lang="javascript">

var resultOfSub = 25 - 102;</source>


Unary Negation

If the value is a string, an attempt is made to convert the string to a number.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>
   </html></source>
   
  

Working With JavaScript Divide Operators

   <source lang="javascript">

<HTML> <HEAD> <TITLE> Working With JavaScript Operators </TITLE> </HEAD> <BODY>

Working With JavaScript Operators

   <SCRIPT LANGUAGE="JavaScript">
   
   </SCRIPT>

</BODY> </HTML></source>