In JavaScript, an operator is a symbol that performs an operation on one or more operands (values or variables). For example, the addition operator + adds two numbers.
let a = 5; let b = 10; let c = a + b; // c is now 15
There are many different types of operators in JavaScript, including arithmetic, assignment, comparison, and logical operators.
Arithmetic Operators
Arithmetic operators perform mathematical operations on numbers. Here are the most commonly used arithmetic operators:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 10 | 15 |
- | Subtraction | 5 - 10 | -5 |
* | Multiplication | 5 * 10 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
Here is an example of using arithmetic operators:
let a = 10; let b = 5; console.log(a + b); // 15 console.log(a - b); // 5 console.log(a * b); // 50 console.log(a / b); // 2 console.log(a % b); // 0 console.log(a ** b); // 100000
Assignment Operators
Assignment operators assign a value to a variable. The most basic assignment operator is the = operator, which assigns the value on the right to the variable on the left:
let a = 5; // a is now 5
There are also compound assignment operators that perform an operation and assign the result to the variable in a single step. For example, the += operator adds the value on the right to the variable on the left and assigns the result to the variable:
let a = 5; a += 10; // a is now 15
Here is a list of all the compound assignment operators:
| Operator | Example | Same As |
|---|---|---|
+= | a += b | a = a + b |
-= | a -= b | a = a - b |
*= | a *= b | a = a * b |
/= | a /= b | a = a / b |
%= | a %= b | a = a % b |
**= | a **= b | a = a ** b |
Comparison Operators
Comparison operators compare two values and return a boolean value (true or false) based on the comparison. Here are the most commonly used comparison operators:
| Operator | Name | Example | Result |
|---|---|---|---|
== | Equal | 5 == 5 | true |
!= | Not equal | 5 != 5 | false |
> | Greater than | 5 > 5 | false |
< | Less than | 5 < 5 | false |
>= | Greater than or equal to | 5 >= 5 | true |
<= | Less than or equal to | 5 <= 5 | true |
Here is an example of using comparison operators:
console.log(5 == 5); // true console.log(5 != 5); // false console.log(5 > 5); // false console.log(5 < 5); // false console.log(5 >= 5); // true console.log(5 <= 5); // true
Logical Operators
Logical operators perform logical operations on boolean values. The most commonly used logical operators are && (and), || (or), and ! (not).
The && operator returns true if both operands are true, and false otherwise:
console.log(true && true); // true console.log(true && false); // false console.log(false && true); // false console.log(false && false); // false
The || operator returns true if either operand is true, and false otherwise:
console.log(true || true); // true console.log(true || false); // true console.log(false || true); // true console.log(false || false); // false
The ! operator negates a boolean value:
console.log(!true); // false console.log(!false); // true
Here is an example of using logical operators in a conditional statement:
let a = 5;
let b = 10;
if (a > 0 && b > 0) {
console.log("Both a and b are positive");
} else {
console.log("Either a or b is not positive");
}
Practice Exercise
Here are some practice questions based on real-world scenarios that could be used to test understanding of the concepts covered in the tutorial on JavaScript operators:
- You are building a simple calculator that adds two numbers together. You need to use the
+operator to add the numbers. What is the result of the following expression:5 + 10? - You are building a shopping cart that allows users to add items to their cart. Each time an item is added, the quantity of that item in the cart is incremented by 1. You need to use the
++operator to increment the quantity. How would you increment the quantity of an item in a cart stored in a variablequantity? - You are building a login system that checks if a user’s entered password is correct. You need to use the
==operator to compare the user’s entered password to the correct password. Is the following expression true or false:"password123" == "password123"? - You are building a to-do list that allows users to mark tasks as complete. You need to use the
&&operator to check if both tasks are marked as complete and the due date has passed. Is the following expression true or false:true && false? - You are building a form that allows users to enter their age. You need to use the
>operator to check if the user’s age is greater than 18. Is the following expression true or false:25 > 18?
I hope this tutorial has helped you understand the different types of operators in JavaScript. Operators are an essential part of programming, and knowing how to use them is crucial for writing efficient and effective code.
