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:

y=5; z=2; x=y+z;

After the above statements are executed,xthe value is:

7

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:

txt1="What a very"; txt2="nice day"; txt3=txt1+txt2;

txt3The result is as follows:

What a verynice day

Try it Yourself »

To add a space between two strings, you need to insert the space into one of the strings:

Examples

txt1="What a very "; txt2="nice day"; txt3=txt1+txt2;

After the above statements are executed, the variabletxt3contains the value:

What a very nice day

Try it Yourself »

Or insert the space into the expression:

Examples

txt1="What a very"; txt2="nice day"; txt3=txt1+" "+txt2;

After the above statements are executed, the variabletxt3contains the value:

What a very nice day

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:

Examples

x=5+5; y="5"+5; z="Hello"+5;

x,y, andzThe output result is:

10
55
Hello5

Try it Yourself »

Rule:If you add a number to a string, the result will become a string!

Other Extensions