JavaScript Tutorial/Operators/Arithmetic operator
Содержание
- 1 += (Addition Assignment)
- 2 + (Addition) with data type conversion
- 3 Add string and integer together
- 4 Append two strings together
- 5 Arithmetic operator in action
- 6 Arithmetic Operators (+)
- 7 Compound assignment operators
- 8 Division (/)
- 9 /= (Division Assignment)
- 10 Multiplication (*)
- 11 *= (Multiplication Assignment)
- 12 String and addition operator (+)
- 13 Subtraction (-)
- 14 Unary Negation
- 15 Working With JavaScript Divide Operators
+= (Addition Assignment)
Syntax
variable += value
+ (Addition) with data type conversion
<html>
<script language="JavaScript">
<!--
aString = new String("67");
answerNum = 67 + 23;
answerStr = aString + 23;
document.write("answerNum =",answerNum,"<br>");
document.write("answerStr =",answerStr);
-->
</script>
</html>
Add string and integer together
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var A = "10", B = 5;
C = A + B;
alert(C);
// -->
</script>
</head>
<body>
</body>
</html>
Append two strings together
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var A = "JavaScript ", B = "by Example!";
C = A + B;
alert(C);
// -->
</script>
</head>
<body>
</body>
</html>
Arithmetic operator in action
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var a = 6, b = 3;
alert(a * b);
// -->
</script>
</head>
<body>
</body>
</html>
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
var resultOfAdd = 34 + 12;
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
var resultOfDiv = 42 / 7;
/= (Division Assignment)
Syntax variable /= value
<html>
<script language="JavaScript">
<!--
x = 32;
aString = new String("8");
x /= aString;
document.write("x = ",x);
-->
</script>
</html>
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
var resultOfMult = 5 * 7;
*= (Multiplication Assignment)
<html>
<script language="JavaScript">
<!--
x = 7;
aString = new String("5");
x *= aString;
document.write("x = ",x);
-->
</script>
</html>
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.
<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var sStringVar1 = "Hello";
var sStringVar2 = "World";
var nNumVar1 = 5;
var nNumVar2 = 10;
var sStringTotal = sStringVar1 + sStringVar2;
var nNumTotal = nNumVar1 + nNumVar2;
var sStringNumTotal = sStringTotal + nNumTotal;
document.write("<b>The string total is: </b>"+sStringTotal+"<BR>");
document.write("<b>The numeric total is: </b>",nNumTotal,"<BR>");
document.write("<b>The string + numeric total is: </b>",sStringNumTotal);
-->
</SCRIPT>
</html>
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:
var resultOfSub = 25 - 102;
Unary Negation
If the value is a string, an attempt is made to convert the string to a number.
<html>
<script language="JavaScript">
<!--
aString = new String("36");
answer = -aString;
document.write("answer = -aString<br>");
document.write("answer = ",answer);
-->
</script>
</html>
Working With JavaScript Divide Operators
<HTML>
<HEAD>
<TITLE>
Working With JavaScript Operators
</TITLE>
</HEAD>
<BODY>
<H1>Working With JavaScript Operators</H1>
<SCRIPT LANGUAGE="JavaScript">
<!--
var pounds = 2.0
var kilograms = pounds / 2.2046
document.write(pounds + " pounds is the same as " + kilograms.toPrecision(4) + " kilograms.")
// -->
</SCRIPT>
</BODY>
</HTML>