JavaScript Logical Operators
Introduction
In JavaScript, logical operators are used to combine multiple conditions or expressions. The two commonly used logical operators are:
- Logical AND (&&): Returns true if both conditions are true.
- Logical OR (||): Returns true if at least one of the conditions is true.
Example 1: Checking for a One-Digit Number
In this example, we will check if the entered number is a one-digit number (from -9 to 9) using the logical AND (`&&`) operator.
Program 1: Check digits using Logical AND
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Check digits using Logical AND</title>
</head>
<body>
<form action="">
<label for="">Enter a number</label>
<input type="number" id="num">
<button type="button" onclick="digits()">Submit</button>
</form>
<script>
function digits(){
var n = document.getElementById("num").value;
if(n<=9 && n>=-9){
document.write("One digit number");
}else{
document.write("Not a one digit number");
}
}
</script>
</body>
</html>
In this program:
- The
digits()function retrieves the number entered in the input field. - The
ifstatement uses the logical AND operator (`&&`) to check if the number is between -9 and 9 (inclusive). If true, it outputs "One digit number". Otherwise, it outputs "Not a one digit number".
Explanation of Logical AND (&&) Operator:
The logical AND operator (`&&`) is used to check if both conditions are true. In the program, the condition n<=9 && n>=-9 checks whether the number n is between -9 and 9, inclusive. Both conditions must be true for the statement to execute.
Example 2: Checking for a Two-Digit Number
In this example, we will check if the entered number is a two-digit number (between 10 and 99 or between -10 and -99) using both logical AND (`&&`) and logical OR (`||`) operators.
Program 2: Check digits using Logical AND and Logical OR
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Check digits using 'Logical AND' and 'Logical OR'</title>
</head>
<body>
<form action="">
<label for="">Enter a number</label>
<input type="number" id="num">
<button type="button" onclick="digits()">Submit</button>
</form>
<script>
function digits(){
var n = document.getElementById("num").value;
if((n<=99 && n>=10) || (n>=-99 && n<=-10)){
document.write("Two digit number");
}else{
document.write("Not a two digit number");
}
}
</script>
</body>
</html>
In this program:
- The
digits()function retrieves the number entered in the input field. - The
ifstatement uses the logical OR operator (`||`) to check if the number is between 10 and 99 or between -10 and -99. If true, it outputs "Two digit number". Otherwise, it outputs "Not a two digit number".
Explanation of Logical OR (||) Operator:
The logical OR operator (`||`) is used to check if at least one of the conditions is true. In this program, the condition (n<=99 && n>=10) || (n>=-99 && n<=-10) checks if the number is between 10 and 99, or between -10 and -99. If either of these conditions is true, the statement will execute.
Conclusion
In summary, JavaScript's logical operators are essential tools for combining multiple conditions in conditional statements. The logical AND (`&&`) operator checks if all conditions are true, while the logical OR (`||`) operator checks if at least one condition is true. Understanding how to use these operators allows you to perform more complex checks in your JavaScript programs.