Courses Learn Blog Contact
Get Started →
Lesson 6

Logical Thinking

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 usernam

JavaScript Simplified 4 min read Free

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.

OperatorMeansTrue when
&&ANDboth sides are true
||ORat least one side is true
!NOTthe value is false (it flips it)

AND: every condition must hold

What this code doesThe && means both checks must be true. Change either value and the welcome turns into the error. This is exactly how a login works.
app.js
1
2
3
4
5
6
7
8
const username = "ada";
const password = "secret123";

if (username === "ada" && password === "secret123") {
  console.log("Welcome, Ada.");
} else {
  console.log("Wrong username or password.");
}
OUTPUT   TERMINAL
➜ webbo3 node app.js
Welcome, Ada.

OR: any condition will do

What this code doesWith ||, only one side needs to be true. "Sunday" matches the second option, so it is the weekend.
app.js
1
2
3
4
5
6
7
const day = "Sunday";

if (day === "Saturday" || day === "Sunday") {
  console.log("It is the weekend.");
} else {
  console.log("It is a weekday.");
}
OUTPUT   TERMINAL
➜ webbo3 node app.js
It is the weekend.

NOT: flip a condition

What this code does!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.
app.js
1
2
3
4
5
const isLoggedIn = false;

if (!isLoggedIn) {
  console.log("Please log in first.");
}
OUTPUT   TERMINAL
➜ webbo3 node app.js
Please log in first.

2. Several conditions together

You can combine more than two. Use brackets to group them so the meaning is clear, just like in maths.

What this code doesThree things must all be true: old enough, has a ticket, and not banned. If any one fails, entry is refused. Reading it aloud is the best way to check it.
app.js
1
2
3
4
5
6
7
8
9
const age = 20;
const hasTicket = true;
const isBanned = false;

if (age >= 18 && hasTicket && !isBanned) {
  console.log("Entry allowed.");
} else {
  console.log("Entry refused.");
}
OUTPUT   TERMINAL
➜ webbo3 node app.js
Entry allowed.
Read conditions out loud

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.

What this code doesFirst an || catches empty fields. Then an && checks the exact pair. Only the right username and password together succeed.
app.js
1
2
3
4
5
6
7
8
9
10
const inputUser = "student";
const inputPass = "webbo3";

if (inputUser === "" || inputPass === "") {
  console.log("Please fill in both fields.");
} else if (inputUser === "student" && inputPass === "webbo3") {
  console.log("Login successful. Welcome!");
} else {
  console.log("Incorrect username or password.");
}
OUTPUT   TERMINAL
➜ webbo3 node app.js
Login successful. Welcome!
Challenge (15 minutes)

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.

Try This

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.

Picture This

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.

Learn this with a real instructor

Free lessons take you a long way on your own. The full programmes add live classes, assignments, Skill Points and a certificate — built for African students.