Arithmetic Operators
Arithmetic Operators
Operator |
Description |
Example |
Result |
+ |
Addition Add |
X = 2 Y = 2 X + y |
4 |
- |
Subtraction Subtraction |
X = 5 Y = 2 X-y |
3 |
* |
Multiplication Multiplication |
X = 5 Y = 4 X * y |
20 |
/ |
Division Division |
15/5 5/2 |
3 2.5 |
% |
Modulus (division remainder) Remainder |
5% 2 10% 8 10% 2 |
1 2 0 |
++ |
Increment Increment |
X = 5 X ++ |
X = 6 |
-- |
Decrement Decrease |
X = 5 X -- |
X = 4 |
Assignment Operators
Value assignment operator
Operator |
Example |
Is The Same |
= |
X = y |
X = y |
+ = |
X + = y |
X = x + y |
-= |
X-= y |
X = x-y |
* = |
X * = y |
X = x * y |
/= |
X/= y |
X = x/y |
% = |
X % = y |
X = x % y |
Comparison Operators
Comparison (relational) Operator
Operator |
Description |
Example |
= |
Is equal Equal |
5 = 8 returns false |
=== |
Is equal to (checks for both value and type) Equal to (check value and type) * all match to be equal |
X = 5 Y = "5"X = y returns true X === y returns false |
! = |
Is not equal Not equal |
5! = 8 returns true |
> |
Is greater Greater |
5> 8 returns false |
< |
Is less Less |
5 <8 returns true |
> = |
Is greater than or equal Greater than or equal |
5> = 8 returns false |
<= |
Is less than or equal Less than or equal |
5 <= 8 returns true |
Logical Operators
Logical operators
Operator |
Description |
Example |
&& |
And And |
X = 6 Y = 3(X <10 & y> 1) returns true |
| |
Or Or |
X = 6 Y = 3(X = 5 | y = 5) returns false |
! |
Not Non |
X = 6 Y = 3! (X = y) returns true |
String Operator
String character (connection function)
A string is most often text, for example "Hello World! ". To stick two or more string variables together, use the + operator.
There are many uses in text, for example, "Hello World !" To link two or more string variables together, you must use the + symbol.
txt1="What a very"txt2="nice day!"txt3=txt1+txt2 |
The variable txt3 now contains "What a verynice day! ".
The txt3 variable now contains "What a verynice day !" (Link 1 and 2)
To add a space between two string variables, insert a space into the expression, OR in one of the strings.
To add a space between two string variables, you must insert a space in the expression or add a space to the variable)
txt1="What a very"txt2="nice day!"txt3=txt1+" "+txt2 or txt1="What a very "txt2="nice day!"txt3=txt1+txt2 |
The variable txt3 now contains "What a very nice day! ".
Now the txt3 variable is "What a very nice day !"
Conditional Operator
Conditional Operators
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
JS has conditional operators that give different values to variables based on different conditions.
Syntax
Syntax
variablename=(condition)?value1:value2 |
Example
Example
greeting=(visitor=="PRES")?"Dear President ":"Dear " |
If the variable visitor is equal to PRES, then put the string "Dear President" in the variable named greeting. if the variable visitor is not equal to PRES, then put the string "Dear" into the variable named greeting.
If the value of the variable visitor is equal to PRES, the value of greeting is "Dear President ". If the value is not PRES, the value of greeting is "Dear"