JavaScript operators are symbols used to perform operations on variables and values. They are the backbone of any logic and arithmetic in your scripts. This post will explore different types of JavaScript operators with examples to illustrate their use.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations, such as addition, subtraction, multiplication, and division.
Operator | Description | Example |
---|---|---|
+ | Addition | let sum = 10 + 5; // 15 |
– | Subtraction | let difference = 10 – 5; // 5 |
* | Multiplication | let product = 10 * 5; // 50 |
/ | Division | let quotient = 10 / 5; // 2 |
% | Modulus | let remainder = 10 % 3; // 1 |
More Information
1. Addition (+)
Adds two values.
let sum = 10 + 5; // 15
2. Subtraction (-)
Subtracts one value from another.
let difference = 10 - 5; // 5
3. Multiplication (*)
Multiplies two values.
let product = 10 * 5; // 50
4. Division (/)
Divides one value by another.
let quotient = 10 / 5; // 2
5. Modulus (%)
Returns the remainder of a division.
let remainder = 10 % 3; // 1
Assignment Operators
Assignment operators assign values to variables, allowing you to set or update the value of a variable.
Operator | Description | Example |
---|---|---|
= | Assignment | let x = 10; |
+= | Addition Assignment | x += 5; // x = x + 5; // 15 |
-= | Subtraction Assignment | x -= 3; // x = x – 3; // 7 |
*= | Multiplication Assignment | x *= 2; // x = x * 2; // 20 |
/= | Division Assignment | x /= 2; // x = x / 2; // 5 |
More Information
1. Assignment (=)
Assigns a value to a variable.
let x = 10;
2. Addition Assignment (+=)
Adds and assigns a value to a variable.
x += 5; // x = x + 5; // 15
3. Subtraction Assignment (-=)
Subtracts and assigns a value to a variable.
x -= 3; // x = x - 3; // 7
4. Multiplication Assignment (*=)
Multiplies and assigns a value to a variable.
x *= 2; // x = x * 2; // 20
5. Division Assignment (/=)
Divides and assigns a value to a variable.
x /= 2; // x = x / 2; // 5
Comparison Operators
Comparison operators compare two values and return a boolean result, allowing you to evaluate conditions in your code.
Operator | Description | Example |
---|---|---|
== | Equal to | let isEqual = (5 == ‘5’); // true |
=== | Strict Equal to | let isStrictEqual = (5 === ‘5’); // false |
!= | Not Equal to | let isNotEqual = (5 != ‘5’); // false |
!== | Strict Not Equal to | let isStrictNotEqual = (5 !== ‘5’); // true |
> | Greater than | let isGreater = (10 > 5); // true |
< | Less than | let isLess = (10 < 5); // false |
>= | Greater than or equal to | let isGreaterOrEqual = (10 >= 10); // true |
<= | Less than or equal to | let isLessOrEqual = (10 <= 5); // false |
More Information
1. Equal to (==)
Checks if two values are equal.
let isEqual = (5 == '5'); // true
2. Strict Equal to (===)
Checks if two values and their types are equal.
let isStrictEqual = (5 === '5'); // false
3. Not Equal to (!=)
Checks if two values are not equal.
let isNotEqual = (5 != '5'); // false
4. Strict Not Equal to (!==)
Checks if two values and their types are not equal.
let isStrictNotEqual = (5 !== '5'); // true
5. Greater than (>)
Checks if one value is greater than another.
let isGreater = (10 > 5); // true
6. Less than (<)
Checks if one value is less than another.
let isLess = (10 < 5); // false
7. Greater than or equal to (>=)
Checks if one value is greater than or equal to another.
let isGreaterOrEqual = (10 >= 10); // true
8. Less than or equal to (<=)
Checks if one value is less than or equal to another.
let isLessOrEqual = (10 <= 5); // false
Logical Operators
Logical operators are used to combine multiple boolean expressions, enabling complex condition evaluations.
Operator | Description | Example |
---|---|---|
&& | Logical AND | let andResult = (true && false); // false |
|| | Logical OR | let orResult = (true || false); // true |
! | Logical NOT | let notResult = !true; // false |
More Information
1. Logical AND (&&)
Returns true if both expressions are true.
let andResult = (true && false); // false
2. Logical OR (||)
Returns true if either expression is true.
let orResult = (true || false); // true
3. Logical NOT (!)
Inverts the boolean value of an expression.
let notResult = !true; // false
Conclusion
JavaScript operators are essential tools for performing various operations in your code. Understanding and using them correctly will help you write more efficient and readable scripts. Experiment with these operators to see how they work and integrate them into your projects.