What are the Most Common JavaScript Operators?
In this article, you will learn about different operators available in JavaScript and how to use them with the help of examples.
What is an Operator?
An operator is a special symbol in JavaScript that is used to perform operations on operands (values and variables).
Example:
4 + 4; // 8
Here +
is an operator that performs addition, and 4
and 4
are operands.
JavaScript Operator Types
Here is a list of different operators you will learn in this tutorial.
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- String Operators
JavaScript Assignment Operators
Assignment operators are used to assign values to variables.
Example:
let x = 10;
Here, the =
operator is used to assign a value of 10
to variable x
.
Here’s a list of commonly used assignment operators:
Operator | Description | Example |
---|---|---|
= | Assignment operator | x = y |
+= | Addition assignment | x += y |
-= | Subtraction Assignment | x -= y |
*= | Multiplication Assignment | x *= y |
/= | Division Assignment | x /= y |
%= | Remainder Assignment | x %= y |
Note: The most commonly used assignment operator is =
.
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic calculations. A typical arithmetic operation operates on two numbers.
Example:
let x = 30 + 50;
// x = 80
Arithmetic operators perform arithmetic on numbers (literals or variables). Here’s a list of commonly used arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Addition | 35 + 5 = 35 |
– | Subtraction | 35 – 5 = 20 |
* | Multiplication | 10 * 20 = 200 |
/ | Division | 20 / 2 = 10 |
% | Modulus | 56 % 3 = 2 |
++ | Increment | let a = 10; a++; Now a =11 |
– – | Decrement | let a = 10; a–; Now a = 9 |
JavaScript Comparison Operators
Comparison operators compare two values and return a boolean value, either true or false. You can check all types of data; comparison operators always return true or false.
Example:
const a = 4, b = 2;
console.log(a > b); // true
Here, the comparison operator >
is used to compare whether a
is greater than b
.
Here’s a list of commonly used comparison operators:
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 10 false |
=== | Identical (equal and of same type) | 5 === 10 false |
!= | Not equal to | 5 != 10 true |
!== | Not identical | 10 !== 10 false |
> | Greater than | 10 > 5 true |
>= | Greater than or equal | 10 >= 5 true |
< | Less than | 10 < 5 false |
<= | Less than or equal to | 10 <= 5 false |
Note: When using operators, be sure that the arguments are of the same data type; numbers should be compared with numbers, strings with strings, and so on.
JavaScript Logical Operators
Logical operators, also known as Boolean operators, perform logical operations and return a boolean value, either true or false.
Example:
(4 > 2) && (10 < 15)
For this expression to be true, both conditions must be true.
- The first condition determines whether 4 is greater than 2, which is true.
- The second condition determines whether 10 is less than 15, which is also true.
- Based on these results, the whole expression is found to be true.
Here’s a list of commonly used logical operators:
Operator | Description | Example |
---|---|---|
&& | Logical AND: true if both the operands are true , else returns true | x && y |
!! | Logical OR: true if either of the operands is true ; returns false if both are false | x !! y |
! | Logical NOT: true if the operand is false and vice-versa. | !x |
Note: You can check all types of data; comparison operators always return true or false.
JavaScript String Operators
The most useful operator for strings is concatenation, represented by the + sign.
Concatenation can be used to build strings by joining together multiple strings, or by joining strings with other types.
Example:
let string1 = "I am learning";
let string2 = "JavaScript.";
let string3 = string1 + " " + string2;
console.log(string3);
//I am learning JavaScript.
The above example declares and initialises two string variables, and then concatenates them.
Note: When + is used with strings, it performs concatenation. However, when + is used with numbers, it performs addition.
Example:
let x = 6 + 6;
let y = "6" + 6;
let z = "Hello" + 88;
/* Output
12
66
Hello88
*/
Note: Numbers in quotes are treated as strings. And If you add a number and a string, the result will be a string!
Conclusion
In this article, we learned about JavaScript operators and their various types. Operators perform arithmetic operations, assign or compare values, evaluate expressions, and more.
Further reading
Want to learn more about JavaScript Operators? Expressions and operators – JavaScript | MDN
See also
How to Comment Code in JavaScript – Best Practices
What are the Three Variables in JavaScript?
What are the Three Primitive JavaScript Data Types?
If you liked this article, then please share. You can also find me on Twitter for more updates.