Logical Operators and Ternary Operator
Logical operators help you make decisions in JavaScript. They are primarily used within if-else statements, loops, and conditional statements where the program must determine what to do next. Learning these operators is important because they improve your logic-building skills, which is very useful for college students and freshers preparing for interviews.
In this chapter, you will learn:
Logical AND (&&)
Logical OR (||)
Logical NOT (!)
The Ternary Operator (?:)
Let us examine them one by one with clear examples.
Logical AND (&&)
AND returns true only if both conditions are true.
console.log(10 > 5 && 8 > 3);
Output:
true
Example with variables:
let age = 20;
let hasID = true;
let canEnter = age >= 18 && hasID;
console.log(canEnter);
Output:
true
If any condition is false:
console.log(10 > 5 && 3 > 10);
Output:
false
Logical OR (||)
OR returns true if any one condition is true.
console.log(10 > 20 || 5 > 2);
Output:
true
Example:
let marks = 30;
let passed = marks >= 35 || marks === 30;
console.log(passed);
Output:
true
One condition is true ? final result is true.
If both conditions are false:
console.log(2 > 5 || 3 > 10);
Output:
false
Logical NOT (!)
NOT reverses the value.
console.log(!true);
console.log(!false);
Output:
false
true
Example:
let isLoggedIn = false;
console.log(!isLoggedIn); // true
NOT is useful when you want the opposite value.
Using Logical Operators Together
You can combine multiple conditions:
let age = 22;
let hasCollegeID = true;
let isStudent = true;
let allowEntry = age >= 18 && hasCollegeID && isStudent;
console.log(allowEntry);
Output:
true
All three conditions are true ? final result is true.
Short-Circuit Behavior (Important for Beginners)
Logical operators sometimes stop evaluating early.
AND (&&)
If the first condition is false, JavaScript does not check the second one.
false && console.log("This will not run");
OR (||)
If the first condition is true, JavaScript skips the rest.
true || console.log("This will not run");
This behavior is very useful in real projects.
Ternary Operator (?:)
The ternary operator is a short way to write an if-else condition.
It makes your code cleaner and easier to read.
Syntax:
condition ? valueIfTrue : valueIfFalse
Example 1
let age = 18;
let message = age >= 18 ? "You can vote": "You cannot vote";
console.log(message);
Output:
You can vote
Example 2
let marks = 40;
let result = marks >= 35 ? "Pass": "Fail";
console.log(result);
Output:
Pass
Example 3: Nested Ternary (Avoid if possible)
let score = 85;
let grade = score >= 90 ? "A" :
score >= 75 ? "B" :
score >= 50 ? "C" : "D";
console.log(grade);
Output:
B
Although it works, beginners should avoid nested ternary for readability.
Example Program Using Logical and Ternary Operators
let age = 20;
let hasID = true;
let canEnter = age >= 18 && hasID;
let message = canEnter ? "Entry allowed" : "Entry denied";
console.log("Can Enter:", canEnter);
console.log("Message:", message);
Output:
Can Enter: true
Message: Entry allowed
Why Logical and Ternary Operators Are Important
You will use them in:
if-else conditions
loops
form validation
filtering data
checking user input
making calculations
building apps
These operators form the foundation of control flow in JavaScript.
Practice Tasks (Do It Yourself)
Check if a student passed using
marks >= 33.Use OR to allow login:
If the user enters a username or an email address, allow login.
Use NOT to reverse a boolean value.
Use ternary to print “Even” or “Odd” for a number.
Use ternary to print the grade based on marks.