Using operators in JavaScript is very similar to how they are used in other programming languages. There are different types of operators you can use in JavaScript. For example, there are operators used for arithmetic, assignment
, comparison, and also concatenation.
Arithmetic Operators
Arithmetic operators are used to perform basic arithmetic between variables and/or values.
Operator | Description | Example> | y=? | x=? |
+ | Addition | x=y+3 | y=5 | x=8 |
- | Subtraction | x=y-3 | y=5 | x=2 |
* | Multiplication | x=y*3 | y=5 | x=15 |
/ | Division | x=y/3 | y=6 | x=2 |
% | Modulus (division remainder) | x=y%3 | y=7 | x=1 |
++ | Increment | x=++y | y=5 | x=6 |
-- | Decrement | x=--y | y=5 | x=4 |
Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Operator | Values | Example | Equivalent to | Result |
= | x=2,y=3 | x=y | x=y | x=3 |
+= | x=2,y=4 | x+=y | x=x+y | x=6 |
-= | x=2,y=5 | x-=y | x=x-y | x=-3 |
*= | x=2,y=6 | x*=y | x=x*y | x=12 |
/= | x=20,y=5 | x/=y | x=x/y | x=4 |
%= | x=7,y=3 | x%=y | x=x%y | x=1 |
Comparison Operators
Comparisons are used to check the relationship between variables and/or values. Comparison operators are used inside conditional statements and for evaluation. The result is either true or false.
Operator | Description | Values | Example | Result |
== | Equal To | x=2,y=3 | x == y | FALSE |
=== | Equal To in Value and Type | x=2,y=3 | x === y | FALSE |
!= | Not Equal To | x=2,y=4 | x != y | TRUE |
!== | Not Equal To in Value nor Type | x=2,y=4 | x !== y | TRUE |
< | Less Than | x=2,y=5 | x < y | TRUE |
> | Greater Than | x=2,y=6 | x > y | FALSE |
<= | Less Than or Equal To | x=2,y=7 | x <= y | TRUE |
>= | Greater Than or Equal To | x=2,y=8 | x >= y | FALSE |
Logical Operators
Logical operators are used to determine the logic between variables or values. Assume in the following example that x=2 and y=3.
Operator | Description | Example | Result |
&& | and | (x < 10 && y > 1) | TRUE |
|| | or | (x==5 || y==4) | FALSE |
! | not | !(x==y) | TRUE |
Concatenation
The + symbol can be used to concatenate two stings or a number and a string. The result of concatenation is a string value. In the following example, assume that string1 has a value of
"John" and that string2 has a value of "Smith".
String #1 | String #2 | Example | Result |
string1 | string2 | string1 + string2 | "John Smith" |
"John" | string2 | "John" + string2 | "John Smith" |
string1 | "Smith" | string1 + "Smith" | "John Smith" |
string1 | string2 | string1 + "A." + string2 | "John
A. Smith" |
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources