JavaScript Operators
= is used to assign values.
+ is used to add values together.
JavaScript Operators:
JavaScript also contains a conditional operator that assigns a value to a variable based on a condition.
/* If the quantity ordered is more than 1000
* then the packing cost is for a large packet,
* otherwise the packing cost is for a small packet.
*/
packingCost=(quantity>1000)?largePacket:smallPacket;
Arithmetic Operators
Arithmetic operators are used to perform arithmetic between variables and/or values.
Operator |
Description |
Example |
---|---|---|
+ |
Addition |
x=y+2; |
- |
Subtraction |
x=y-2; |
* |
Multiplication |
x=y*2; |
/ |
Division |
x=y/2; |
% |
Modulus (division remainder) |
x=y%2; |
++ |
Pre-Increment Post-Increment |
x=++y; x=y++; |
-- |
Pre-Decrement Post-Decrement |
x=--y; x=y--; |
Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Operator |
Example |
Equivalent to |
---|---|---|
= |
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 operators are used in logical statements to determine equality or difference between variables or values.
Operator |
Description |
---|---|
== |
equal to |
=== |
exactly equal to (value and type) |
!= |
not equal |
!== |
not exactly equal (different value or type) |
> |
greater than |
>= |
greater than or equal to |
< |
less than |
<= |
less than or equal to |
Logical Operators
Logical operators are used to determine the logic between variables or values.
Operator |
Description |
Example |
---|---|---|
&& |
and |
(true && false) == false |
|| |
or |
(true || false) == true |
! |
not |
!(false) == true |