Operators
Variables are used for storing values or expressions.
Assignment Operators
JavaScript supports the following assignment operators:
Operator | Description |
---|---|
= |
Assignment |
+= |
Add and assign |
-= |
Subtract and assign |
*= |
Multiply and assign |
/= |
Divide and assign |
%= |
Modulus and assign |
Arithmetic Operators
JavaScript supports the following arithmetic operators:
Operator | Description |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus (division remainder) |
++ |
Increment |
-- |
Decrement |
In JavaScript, the '+' operator is overloaded; it is used for string concatenation, arithmetic addition, and also to convert strings to numbers.
String Concatenation
To add two or more string variables together, use the + operator:
msg1 = "First Message.";
msg2 = "Second Message.";
output = msg1 + " " + msg2;
After these statements execute, output would contain:
First Message. Second Message.
Adding Strings and Numbers
When adding a number and a string in JavaScript, the result will always be a string:
sum = 50 + 50;
// sum contains 100 (a number)
sum = "50" + 50;
// sum contains "5050" (a string)