Javascript Tutorial-12 Relational And Logical Operator
In the world of programming, operators play a crucial role in performing various operations and manipulating data. In JavaScript, operators are used to carry out different tasks such as mathematical calculations, logical evaluations, and comparisons. In this tutorial, we will dive deep into the realm of relational and logical operators in JavaScript. Understanding these operators is essential for writing efficient and effective code. So, let's explore the intricacies of relational and logical operators and how they can be utilized in JavaScript programming.
Relational Operators
What are Relational Operators?
Relational operators, also known as comparison operators, allow us to compare values and determine the relationship between them. These operators return a Boolean value (true or false) based on the result of the comparison.
Types of Relational Operators
JavaScript provides several relational operators that enable us to perform different types of comparisons. Let's take a closer look at each of these operators:
1. Equal to (==) Operator
The equal to operator compares two values for equality and returns true if they are equal, and false otherwise. It performs type coercion, which means it converts the operands to a common type before making the comparison.
For example:
let x = 5;
let y = 10;
console.log(x == y); // Output: false
2. Not Equal to (!=) Operator
The not equal to operator checks if two values are not equal and returns true if they are not equal, and false if they are equal.
For example:
let x = 5;
let y = 10;
console.log(x != y); // Output: true
3. Strict Equal to (===) Operator
The strict equal to operator compares two values for both equality and data type. It returns true if both the values are equal and of the same type, and false otherwise.
For example:
let x = 5;
let y = "5";
console.log(x === y); // Output: false
4. Strict Not Equal to (!==) Operator
The strict not equal to operator checks if two values are either not equal or not of the same type. It returns true if both conditions are met, and false if the values are equal and of the same type.
For example:
let x = 5;
let y = "5";
console.log(x !== y); // Output: true
5. Greater than (>) Operator
The greater than operator compares two values and returns true if the left operand is greater than the right operand, and false otherwise.
For example:
let x = 5;
let y = 10;
console.log(x > y); // Output: false
6. Less than (<) Operator
The less than operator checks if the left operand is less than the right operand. It returns true if the condition is satisfied, and false otherwise.
For example:
let x = 5;
let y = 10;
console.log(x < y); // Output: true
7. Greater than or equal to (>=) Operator
The greater than or equal to operator compares two values and returns true if the left operand is greater than or equal to the right operand, and false otherwise.
For example:
let x = 5;
let y = 10;
console.log(x >= y); // Output: false
8. Less than or equal to (<=) Operator
The less than or equal to operator checks if the left operand is less than or equal to the right operand. It returns true if the condition is satisfied, and false otherwise.
For example:
let x = 5;
let y = 10;
console.log(x <= y); // Output: true
Relational Operators in Action
Now that we have a good understanding of the different relational operators available in JavaScript, let's see how they can be used in practical scenarios.
Comparing Numbers
Relational operators are commonly used to compare numerical values. We can use these operators to check if one number is greater than, less than, or equal to another number.
let num1 = 5;
let num2 = 10;
console.log(num1 > num2); // Output: false
console.log(num1 < num2); // Output: true
console.log(num1 === num2); // Output: false
console.log(num1 !== num2); // Output: true
Comparing Strings
Relational operators can also be used to compare strings. The comparison is performed based on the lexicographical order of the characters.
let str1 = "apple";
let str2 = "banana";
console.log(str1 > str2); // Output: false
console.log(str1 < str2); // Output: true
console.log(str1 === str2); // Output: false
console.log(str1 !== str2); // Output: true
Combining Relational Operators
We can combine multiple relational operators to form complex comparisons. The logical AND (&&) and logical OR (||) operators can be used for this purpose.
let num1 = 5;
let num2 = 10;
let num3 = 7;
console.log(num1 < num2 && num2 < num3); // Output: false
console.log(num1 < num2 || num2 < num3); // Output: true
Logical Operators
What are Logical Operators?
Logical operators in JavaScript are used to perform logical operations on Boolean values. These operators allow us to combine conditions and make decisions based on the combined result.
Types of Logical Operators
JavaScript provides three logical operators: AND (&&), OR (||), and NOT (!). Let's explore each of these operators in detail.
1. Logical AND (&&) Operator
The logical AND operator returns true if both the operands are true, and false otherwise. It evaluates the second operand only if the first operand is true.
For example:
let x = 5;
let y = 10;
console.log(x < 10 && y > 5); // Output: true
console.log(x < 10 && y < 5); // Output: false
2. Logical OR (||) Operator
The logical OR operator returns true if at least one of the operands is true, and false if both operands are false. It evaluates the second operand only if the first operand is false.
For example:
let x = 5;
let y = 10;
console.log(x > 10 || y < 5); // Output: false
console.log(x < 10 || y < 5); // Output: true
3. Logical NOT (!) Operator
The logical NOT operator negates the truth value of its operand. It returns true if the operand is false, and false if the operand is true.
For example:
let x = 5;
console.log(!(x === 5)); // Output: false
console.log(!(x !== 5)); // Output: true
Logical Operators in Action
Now, let's explore some practical examples of using logical operators in JavaScript.
Conditional Statements
Logical operators are often used in conditional statements to control the flow of the program based on different conditions.
let age = 25;
let hasLicense = true;
if (age >= 18 && hasLicense) {
console.log("You are eligible to drive.");
} else {
console.log("You are not eligible to drive.");
}
In the above example, the program checks if the age is 18 or above and if the person has a valid driver's license. If both conditions are true, the program displays the message "You are eligible to drive." Otherwise, it displays "You are not eligible to drive."
Short-Circuit Evaluation
Logical operators also employ short-circuit evaluation. This means that the second operand is only evaluated if necessary.
let x = 5;
console.log(x > 0 && x < 10); // Output: true
console.log(x > 10 && x < 5); // Output: false (Short-circuit evaluation)
In the second example, since the first operand is already false, there is no need to evaluate the second operand. The result is determined based on the first operand, resulting in false without evaluating the second expression.
Frequently Asked Questions (FAQs)
- The
==
operator checks for equality after performing type coercion, while the===
operator checks for equality without type coercion. This means that===
is more strict and requires both the values and the types to be the same for the comparison to be true.
- You can use the relational operators (
<
,>
,<=
,>=
) to compare strings in JavaScript. The comparison is based on the lexicographical order of the characters.
- Short-circuit evaluation is a behavior in logical operators where the second operand is only evaluated if the first operand is not sufficient to determine the result. This behavior can be useful for improving performance and avoiding unnecessary computations.
- Yes, you can combine multiple logical operators in JavaScript to form complex conditions. This allows you to create more advanced logical expressions and make decisions based on multiple conditions.
- Logical operators are often used in conditional statements (
if
,else if
,else
) to check multiple conditions and control the flow of the program based on those conditions. By combining logical operators, you can create complex conditions for conditional statements.
- The logical NOT operator (
!
) in JavaScript is used to negate the truth value of its operand. It returnstrue
if the operand isfalse
, andfalse
if the operand istrue
. It is commonly used to reverse the truth value of a condition or expression.
Conclusion
In this tutorial, we explored the world of relational and logical operators in JavaScript. Relational operators allow us to compare values and determine relationships, while logical operators help us combine conditions and make decisions based on the combined result. Understanding these operators is crucial for writing robust and efficient JavaScript code. By utilizing these operators effectively, you can enhance the logic and control flow of your programs. So go ahead, practice using relational and logical operators, and unlock the full potential of JavaScript programming!