Arithmetic Operators
Arithmetic operators
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition add |
x=2 y=2 x+y |
4 |
- | Subtraction reduce |
x=5 y=2 x-y |
3 |
* | Multiplication take |
x=5 y=4 x*y |
20 |
/ | Division remove |
15/5 5/2 |
3 2.5 |
% | Modulus (division remainder) Remaining number |
5%2 10%8 10%2 |
1 2 0 |
++ | Increment Increment |
x=5 x++ |
x=6 |
-- | Decrement Decreasing |
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
Comparative (relational) operator
Operator | Description | Example |
---|---|---|
== | is equal to equal |
5==8 returns false |
=== | is equal to (checks for both value and type) Equal to (check value and type) * is equal to |
x=5 y="5" x==y returns true |
!= | 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 < 10 && y > 1) returns true |
|| | or or |
x=6 y=3 (x==5 || y==5) returns false |
! | not No |
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.
It is used more frequently in text. For example, if "Hello World!" is to connect 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 very day!" (concatenates 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 (space) to one of them.
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 condition.
JS has conditional operators that give variables different values according to different conditions
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 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 not PRES, then the greeting value is "Dear"