Js Logical & Assignment Operators
Logical operators
In JavaScript, logical operators are used to combine or manipulate boolean values (true or false). There are three main logical operators: &&
(AND), ||
(OR), and !
(NOT). Let's explore each of these operators with some code examples:
1. AND Operator (&&
):
The &&
operator returns true
if both operands are true
. Otherwise, it returns false
.
let age = 25;
let isCitizen = true;
if (age >= 18 && isCitizen) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}
2. OR Operator (||
):
The ||
operator returns true
if at least one of the operands is true
. If both operands are false
, it returns false
.
let hasAccount = true;
let hasSubscription = false;
if (hasAccount || hasSubscription) {
console.log("You have access to the content.");
} else {
console.log("You need an account or subscription to access the content.");
}
3. NOT Operator (!
):
The !
operator negates the value of its operand. It converts true
to false
and false
to true
.
let isLoggedIn = false;
if (!isLoggedIn) {
console.log("Please log in to access the dashboard.");
} else {
console.log("Welcome to the dashboard!");
}
4. Combining Logical Operators:
You can also combine logical operators to create more complex conditions.
let hasPermission = true;
let isMember = false;
let isPremium = true;
if ((hasPermission || isMember) && isPremium) {
console.log("You have access to the premium features.");
} else {
console.log("Upgrade to a premium membership to access premium features.");
}
5. Short-Circuit Evaluation:
JavaScript employs short-circuit evaluation with logical operators. This means that if the value of the expression can be determined by only evaluating the first operand, the second operand will not be evaluated. For example:
let result = true || someFunction(); // someFunction() is not called because the first operand is true
Remember that logical operators work with boolean values or expressions that can be evaluated to boolean values. They are commonly used for making decisions based on multiple conditions or for controlling the flow of your code based on boolean logic.
Assignment operators
Assignment operators in JavaScript are used to assign values to variables. They combine the process of assigning a value with an operation, making it more concise. Here are some common assignment operators along with code examples:
1. Assignment Operator (=): This is the basic operator that assigns a value to a variable.
let x = 5;
let y = 10;
2. Addition Assignment (+=): Adds a value to a variable and assigns the result back to the same variable.
let num = 5;
num += 3; // Equivalent to num = num + 3;
console.log(num); // Output: 8
3. Subtraction Assignment (-=): Subtracts a value from a variable and assigns the result back to the same variable.
let total = 20;
total -= 5; // Equivalent to total = total - 5;
console.log(total); // Output: 15
4. Multiplication Assignment (*=): Multiplies a variable by a value and assigns the result back to the same variable.
let quantity = 3;
quantity *= 4; // Equivalent to quantity = quantity * 4;
console.log(quantity); // Output: 12
5. Division Assignment (/=): Divides a variable by a value and assigns the result back to the same variable.
let price = 50;
price /= 2; // Equivalent to price = price / 2;
console.log(price); // Output: 25
6. Modulus Assignment (%=): Performs modulus operation on a variable and assigns the remainder back to the same variable.
let number = 17;
number %= 5; // Equivalent to number = number % 5;
console.log(number); // Output: 2
7. Exponentiation Assignment (**=): Raises a variable to the power of a value and assigns the result back to the same variable.
let base = 2;
base **= 3; // Equivalent to base = base ** 3;
console.log(base); // Output: 8