JavaScript Operators
= is used to assign values.
+ is used to add values.
The assignment operator = is used to assign values to JavaScript variables.
The arithmetic operator + is used to add values together.
y=5;
z=2;
x=y+z;
The value of x, after the execution of the statements above is 7.
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic beteen variables and!or values.
"iven that y=5, the table belo explains the arithmetic operators#
Operator Description Example Result
+ Addition x=$+% x=7
& Subtraction x=$&% x='
( )ultiplication x=$(% x=*+
! ,ivision x=$!% x=%.-
. )odulus /division remainder0 x=$.% x=*
++ 1ncrement x=++$ x=2
&& ,ecrement x=&&$ x=3
JavaScript Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
"iven that x=10 and y=5, the table belo explains the assignment operators#
Operator Example Same As Result
= x=$ x=-
+= x+=$ x=x+$ x=*-
&= x&=$ x=x&$ x=-
(= x(=$ x=x($ x=-+
!= x!=$ x=x!$ x=%
.= x.=$ x=x.$ x=+
The + Operator 4sed on Strings
The + operator can also be used to add string variables or text values together.
To add to or more string variables together, use the + operator.
txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;
After the execution of the statements above, the variable txt' contains 56hat a ver$nice da$5.
To add a space beteen the to strings, insert a space into one of the strings#
txt1="What a very ";
txt2="nice day";
txt3=txt1+txt2;
or insert a space into the expression#
txt1="What a very";
txt2="nice day";
txt3=txt1+" "+txt2;
After the execution of the statements above, the variable txt' contains#
56hat a ver$ nice da$5
Adding Strings and 7umbers
The rule is# If you add a number and a string t!e result "ill be a string#
8xample
x=5+5;
document.write(x);
x="5"+"5";
document.write(x);
x=5+"5";
document.write(x);
x="5"+5;
document.write(x);