JavaScript Operators
The operator = is used for assignment.
The operator + is used to add values.
The operator = is used to assign values to JavaScript variables.
Arithmetic Operators+Used to add values together.
Examples
Assign variable values, and add the values together:
After the above statements are executed,xthe value is:
Try it Yourself »
JavaScript Arithmetic Operators
and/orarithmetic operations between values.
y=5, the following table explains these arithmetic operators:
| Operators | Description | Example | x Result | y Result | Online Examples |
|---|---|---|---|---|---|
| + | Addition | x=y+2 | 7 | 5 | Example » |
| - | Subtraction | x=y-2 | 3 | 5 | Example » |
| * | Multiplication | x=y*2 | 10 | 5 | Example » |
| / | Division | x=y/2 | 2.5 | 5 | Example » |
| % | Modulus (Remainder) | x=y%2 | 1 | 5 | Example » |
| ++ | Increment | x=++y | 6 | 6 | Example » |
| x=y++ | 5 | 6 | Example » | ||
| -- | Decrement | x=--y | 4 | 4 | Example » |
| x=y-- | 5 | 4 | Example » |
JavaScript Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Givenx=10 and y=5, the following table explains the assignment operators:
| Operators | Example | Equivalent to | Result | Online Examples |
|---|---|---|---|---|
| = | x=y | x=5 | Example » | |
| += | x+=y | x=x+y | x=15 | Example » |
| -= | x-=y | x=x-y | x=5 | Example » |
| *= | x*=y | x=x*y | x=50 | Example » |
| /= | x/=y | x=x/y | x=2 | Example » |
| %= | x%=y | x=x%y | x=0 | Example » |
The + Operator Used on Strings
The + operator is used to add (concatenate) text values or string variables.
To concatenate two or more string variables, use the+operator.
Examples
To concatenate two or more string variables, use the+operator:
txt3The result is as follows:
Try it Yourself »
To add a space between two strings, you need to insert the space into one of the strings:
Examples
After the above statements are executed, the variabletxt3contains the value:
Try it Yourself »
Or insert the space into the expression:
Examples
After the above statements are executed, the variabletxt3contains the value:
Try it Yourself »
Adding Strings and Numbers
Adding two numbers returns the sum of the numbers. If you add a number and a string, it returns a string, as in the following example:
Rule:If you add a number to a string, the result will become a string!
Other Extensions