In JavaScript, operators are symbols or keywords used to perform operations on operands (values or variables). They can manipulate data, perform calculations, or compare values. Here’s an overview of common JavaScript operators for beginners:
1. Arithmetic Operators
Used for performing mathematical calculations.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 5 / 3 |
1.67 |
% |
Modulus (remainder) | 5 % 3 |
2 |
** |
Exponentiation | 5 ** 3 |
125 |
2. Assignment Operators
Used to assign values to variables.
Operator | Description | Example | Result |
---|---|---|---|
= |
Assignment | x = 5 |
x is 5 |
+= |
Addition assignment | x += 3 |
x = x + 3 |
-= |
Subtraction assignment | x -= 3 |
x = x - 3 |
*= |
Multiplication assignment | x *= 3 |
x = x * 3 |
/= |
Division assignment | x /= 3 |
x = x / 3 |
%= |
Modulus assignment | x %= 3 |
x = x % 3 |
3. Comparison Operators
Used to compare values and return a boolean (true
or false
).
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | 5 == "5" |
true |
=== |
Strict equal (value and type) | 5 === "5" |
false |
!= |
Not equal | 5 != "5" |
false |
!== |
Strict not equal | 5 !== "5" |
true |
> |
Greater than | 5 > 3 |
true |
< |
Less than | 5 < 3 |
false |
>= |
Greater than or equal to | 5 >= 3 |
true |
<= |
Less than or equal to | 5 <= 3 |
false |
4.Logical Operators
Used to perform logical operations and combine multiple conditions.
Operator | Description | Example | Result |
---|---|---|---|
&& |
Logical AND | true && false |
false |
` | ` | Logical OR | |
! |
Logical NOT | !true |
false |
5. Bitwise Operators
Operate on binary representations of numbers.
Operator | Description | Example | Result |
---|---|---|---|
& |
AND | 5 & 3 |
1 |
` | ` | OR | `5 |
^ |
XOR | 5 ^ 3 |
6 |
~ |
NOT | ~5 |
-6 |
<< |
Left shift | 5 << 1 |
10 |
>> |
Right shift | 5 >> 1 |
2 |
6. String Operators
Used to concatenate strings.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Concatenation | "Hello" + " World" |
"Hello World" |
7. Type Operators
Used to determine or convert types.
Operator | Description | Example | Result |
---|---|---|---|
typeof |
Returns the type of a variable | typeof 5 |
"number" |
instanceof |
Checks if an object is an instance of a class | x instanceof Array |
true or false |
8. Conditional (Ternary) Operator
A shorthand for if
–else
.
Operator | Description | Example | Result |
---|---|---|---|
condition ? expr1 : expr2 |
Returns expr1 if the condition is true , otherwise expr2 |
5 > 3 ? "Yes" : "No" |
"Yes" |
9. Comma Operator
Evaluates multiple expressions and returns the last expression.
Operator | Description | Example | Result |
---|---|---|---|
, |
Comma | x = (2, 3, 4) |
4 |
Summary
JavaScript operators are essential tools for performing operations on variables and values. By understanding these operators, beginners can effectively manipulate data, perform calculations, and control the flow of their programs.