What are Logical Operators?
Logical operators are used to combining two or more boolean expressions and evaluate them as a single boolean value. JavaScript has three logical operators: &&
(logical AND), ||
(logical OR), and !
(logical NOT).
&&
(logical AND):😃 Returnstrue
if both expressions are true. For example,true && true
returnstrue
, buttrue && false
andfalse && false
returnfalse
.||
(logical OR): 😊Returnstrue
if at least one of the expressions is true. For example,true || false
andfalse || true
returntrue
, butfalse || false
returnsfalse
.!
(logical NOT): 🤐Returns the opposite of the boolean value of the expression. For example,!true
returnsfalse
, and!false
returnstrue
.
Logical AND Operator.
The logical AND operator (&&) is used to evaluate two operands and returns true if both the operands are true, otherwise, it returns false.
Syntax
operand1 && opearnd2
Here, operand1 and operand2 are the two expressions or values that we want to compare.
The logical AND operator evaluates the operands from left to right. If the first operand is falsy, then it returns the first operand. Otherwise, it evaluates the second operand and returns it.
Example: Let's say you have a function that checks if a person is eligible to vote in a country based on their age and citizenship status:
function isEligibleToVote(age, isCitizen) {
if (age >= 18 && isCitizen) {
return true;
} else {
return false;
}
}
console.log(isEligibleToVote(20, true)); // true
console.log(isEligibleToVote(16, true)); // false
console.log(isEligibleToVote(20, false)); // false
Here, the &&
the operator is used to combine two conditions: the age must be greater than or equal to 18, and the person must be a citizen. If both conditions are true, the function returns true
and the person is eligible to vote. Otherwise, the function returns false
.
For example, if you call the function with the arguments isEligibleToVote(20, true)
, it will return true
because the person is 20 years old and a citizen, which meets both conditions. However, if you call the function with the arguments isEligibleToVote(16, true)
, it will return false
because the person is not yet 18 years old, which fails the first condition.
Why we use "is" before the function name:😵
Example: In JavaScript, putting "is" before a function name is a common naming convention used for functions that return a boolean value (true or false). This convention helps to make the code more readable and self-explanatory.
function isEven(number) {
return number % 2 === 0;
}
Here, the function name "isEven" follows the convention of starting with "is" because it returns a boolean value indicating whether the input number is even or not.
Here, the function name "isEven" follows the convention of starting with "is" because it returns a boolean value indicating whether the input number is even or not.
Logical OR Operator.
The logical OR operator in JavaScript is represented by ||
. It is used to combine two or more expressions and returns true
if at least one of the expressions is true
.
Example:🎇
let age = 25;
let isStudent = false;
if (age < 18 || isStudent) {
console.log("You are not eligible to vote.");
} else {
console.log("You are eligible to vote.");
}
In this example, the OR operator is used to combine two expressions: age < 18
and isStudent
. The condition inside the if statement evaluates to true
if either of these expressions is true
.
In this case, since age
is greater than 18, the first expression age < 18
is false
. However, since isStudent
is false
, the second expression is also false
. Since both expressions are false
, the entire condition evaluates to false
and the message "You are eligible to vote." is printed.
If age
had been less than 18, the first expression age < 18
would have evaluated to true
, and the entire condition would have evaluated to true
, resulting in the message "You are not eligible to vote." being printed. If isStudent
had been true
, the second expression would have evaluated to true
, and the entire condition would have evaluated to true
, resulting in the message "You are not eligible to vote." being printed.
Logical NOT Operator.
The logical NOT operator is represented by an exclamation mark (!) in JavaScript. It is a unary operator that converts its operand to a boolean value and returns the opposite boolean value.
When you use the logical NOT operator with a value, the value is first converted to a boolean value. If the value is truthy (i.e., it's not false, 0, "", null, undefined, or NaN), the operator returns false. If the value is falsy (i.e., it's false, 0, "", null, undefined, or NaN), the operator returns true.
Example:
let x = 2;
console.log(!x);
//Here x means 2 and 2 is truthy value so, ! operator converts truthy value into the falsy and give result in a boolean value.......
You can also use the logical NOT operator with expressions that evaluate to a boolean value. Here is an example:
let isTrue = true;
console.log(!isTrue); // Output: false
let isFalse = false;
console.log(!isFalse); // Output: true
let hasValue = "hello";
console.log(!hasValue); // Output: false
let noValue = "";
console.log(!noValue); // Output: true
Truthy falsy:
In JavaScript, a value is considered truthy if it is considered true when encountered in a boolean context. On the other hand, a value is considered falsy if it is considered false when encountered in a boolean context.
There are six falsy values in JavaScript:
false
null
undefined
0
NaN
''
(empty string)
All other values in JavaScript are truthy, including:
true
All non-zero numbers, including negative numbers and decimals
All non-empty strings, including whitespace characters
Empty objects (
{}
) and arrays ([]
)Functions