JavaScript Simplified · One concept, one day, one project · Day 6 of 32
Logical Thinking
Combining conditions so a program can weigh more than one thing at once. You build a login checker that only lets the right username and password through.
By the end of today you can
- Combine conditions with AND, OR and NOT
- Choose between && and || for a real requirement
- Read a condition with several parts without getting lost
- Build a login checker
Yesterday each decision rested on one question. Real life is fussier: you are let in only if the username is right and the password is right. Today you combine conditions, which is where logic starts to feel powerful.
1. AND, OR, NOT
You met these on Day 4. Now you use them inside decisions.
| Operator | Means | True when |
|---|---|---|
&& | AND | both sides are true |
|| | OR | at least one side is true |
! | NOT | the value is false (it flips it) |
AND: every condition must hold
&& means both checks must be true. Change either value and the welcome turns into the error. This is exactly how a login works.OR: any condition will do
||, only one side needs to be true. "Sunday" matches the second option, so it is the weekend.NOT: flip a condition
!isLoggedIn reads as "not logged in". Since isLoggedIn is false, !isLoggedIn is true, so the message prints. A clean way to act when something is NOT the case.2. Several conditions together
You can combine more than two. Use brackets to group them so the meaning is clear, just like in maths.
A long condition is easier to trust when you say it in words: "if age is at least 18 AND they have a ticket AND they are not banned". If the sentence makes sense, the code usually does too.
Today’s Mini Project
🔐 Login Checker
Check a username and password together. Only the correct pair gets in; anything else is refused. Add a small touch: refuse early if either field is empty.
|| catches empty fields. Then an && checks the exact pair. Only the right username and password together succeed.Add a third correct account (a different username and password) that also logs in successfully, without breaking the first one. Expected: two different valid logins both work, everything else is refused.
Write a check that prints "Free entry" when someone is under 13 || over 65, and "Pay the fee" otherwise.
Expected: the right message for ages like 10, 40 and 70.
A real login, a real door policy, a real "are you allowed to do this" check: all of them are a handful of true/false questions joined with AND, OR and NOT. You can now express the kind of rule that runs real systems.
Checkpoint
When is an && condition true?
Only when both sides are true. If either side is false, the whole thing is false. Use it when every requirement must hold.
What does !isLoggedIn mean?
It flips the value: it is true when isLoggedIn is false. It reads as "not logged in", so you can act when something is not the case.
Why group conditions with brackets?
To make the order and meaning clear, so JavaScript and the reader both group the parts the way you intended.
Tick Before You Move On
- ☐ I can require several things at once with &&
- ☐ I can accept any of several options with ||
- ☐ I can act when something is not true using !
- ☐ I built the Login Checker
Quick Recap
Logical operators combine conditions so a program can weigh several things. && needs all sides true; || needs at least one; ! flips true and false. Brackets group conditions so the meaning is clear. Reading a condition aloud is the quickest way to check it is right.
Next: Day 7. Some decisions are so small that a full if/else feels heavy. The ternary operator writes a simple choice in one line.