Arithmetic Operators
Arithmetic operators
Operator |
Description |
Example |
| Result
+ |
Addition Add |
x=2 y=2 X+y |
4 |
- |
Subtraction Reducing |
X=5 y=2 X-y |
3 |
* |
Multiplication By |
X=5 Y=4 X*y |
20 |
/ |
Division Except |
15/5 5/2 |
3 2.5 |
% |
Modulus (Division remainder) Remainder |
5%2 10%8 10%2 |
1 2 0 |
++ |
Increment Increasing |
X=5 X + + |
X=6 |
-- |
Decrement Decline |
X=5 x-- |
X=4 |
Assignment Operators
Assignment operator
Operator |
Example | is the
Same | as
= |
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 to Equals |
5==8 returns False |
=== |
is equal to (checks for both value and type) Equal to (check value and type) * All matches are equal |
X=5 Y= "5"X==y returns True X===y returns False |
!= |
is not equal Not equal to |
5!=8 returns True |
> |
is greater than Greater than |
5>8 returns False |
< |
is less than Less than |
5<8 returns True |
>= |
is greater than or equal to Greater than or equal to |
5>=8 returns False |
<= |
is less than or equal to Less than or equal to |
5<=8 returns True |
Logical Operators
logical operators
Operator |
Description |
Example |
&& |
and And |
X=6 Y=3(x < ten && 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 (Join action)
A string is most often text, for example "Hello world!". To stick two or more string variables together and use the + operator.
Use more of the text, for example, "Hello world!" To join two or more string variables together, you have to use the + symbol
Txt1= "What a very"
txt2= "Nice day!"
Txt3=txt1+txt2 |
The variable txt3 now contains "What a Verynice day!".
TXT3 variable now contains "What a Verynice day!" (Connecting the 1 and the 2)
To add a space between two string variables, insert a spaces into the expression, or in one of the strings.
To add a space to the middle of two string variables, insert a space in the expression or one of them (spaces)
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 variable txt3 is "What a very nice day!"
Conditional Operator
Conditional operator
JavaScript also contains a conditional operator that assigns a value to a variable based on some.
JS has conditional operators that give different values to variables depending on the condition
Syntax
Grammar
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. If The variable visitor is isn't equal to PRES, then put the string "Dear" into the variable named.
If the value of the variable visitor is equal to Pres then the greeting value is "Dear President". If it is not for Pres then the greeting value is "Dear"