5. Making Decisions
5 Making Decisions
5.1 if, else if, else
Programs become truly useful when they can make decisions. Instead of running the same instructions every time, your code can choose different paths depending on the situation — just as you do in everyday life. In JavaScript and Google Apps Script, the primary tool for decision-making is the if statement.
We’re still at the BYU-Idaho University Store from the last chapter, but now the question has changed. We know how many clickers to order at a time (the EOQ of 479). The new questions are when to place that order, and which of the store’s many items deserve the closest attention. Both turn out to be decisions of the same shape: compare the situation to a threshold, and act on the answer. The clicker’s threshold is its reorder point — about 12 clickers sell per day, and a delivery takes about 10 days to arrive, so roughly 120 sell while we wait; add a 30-unit cushion for weeks that run hot and the reorder point is 150. (That cushion is “safety stock,” and pinning it down precisely takes demand variability we’ll reach later; for now, 150 is the number to beat.)
Think of if, else if, and else
as a branching path. Your program checks a condition and, based on
whether it’s true or false, decides what to do next.
5.1.1 The Basic if
Statement
An if statement checks a condition. If the condition is
true, the code inside the block runs. If it’s
false, the block is skipped.
let onHand = 120
if (onHand <= 150) {
console.log("Time to reorder")
}
Here the message prints only if onHand <= 150 — the
reorder point. Because 120 is at or below 150, it does: the clickers
have dropped far enough that it’s time to order.
5.1.2 Adding More Options With
else if
Sometimes one condition isn’t enough. You may want to check several
possibilities in order. That’s where else if comes in.
let onHand = 220
if (onHand <= 150) {
console.log("Reorder now")
} else if (onHand <= 300) {
console.log("Getting low — keep an eye on it")
}
The program checks the first condition. If it’s false, it moves on to the next. Only the block tied to the first condition that evaluates to true runs.
5.1.3 Catching Everything Else With
else
The else block runs when none of the previous conditions
are true. It’s your fallback.
let onHand = 480
if (onHand <= 150) {
console.log("Reorder now")
} else if (onHand <= 300) {
console.log("Getting low — keep an eye on it")
} else {
console.log("Stock is healthy")
}
This structure guarantees that exactly one message prints, whatever the stock level.
5.1.4 How This Applies in Apps Script
Decision-making becomes especially powerful with spreadsheet data. You might check whether a cell is empty, decide whether a value crosses a threshold, sort items into groups, or trigger different actions. Here’s a script that reads an item’s on-hand quantity from cell A1 and writes a status into B1:
function checkStock() {
let sheet = SpreadsheetApp.getActiveSheet()
let onHand = sheet.getRange("A1").getValue()
if (onHand <= 150) {
sheet.getRange("B1").setValue("Reorder now")
} else if (onHand <= 300) {
sheet.getRange("B1").setValue("Getting low")
} else {
sheet.getRange("B1").setValue("Healthy")
}
}
This is the heart of automation: your script looks at real data and responds intelligently.
5.1.5 Why if
Statements Matter
With if, else if, and else,
your programs stop being static and start becoming smart. They
can react to changing stock, choose between actions, validate input,
control the flow of a script, and make your spreadsheet tools feel
responsive. These decision-making tools are the foundation for
everything from simple checks to complex policy. Next, you’ll learn to
combine conditions, compare values, and build more sophisticated
decisions.
5.2 Comparison and Logical Operators
Decision-making depends on evaluating conditions — questions your
program asks to decide what to do next. To express those questions, you
use comparison operators and logical
operators. They let your code compare values, combine
conditions, and determine whether something is true or false. Once you
understand them, your if statements become far more
expressive.
5.2.1 Comparison Operators
Comparison operators check how two values relate. They always produce
a boolean — either true or
false. The most common ones:
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== |
equal to (strict) | 5 === 5 |
true |
!== |
not equal to (strict) | 4 !== 5 |
true |
== |
equal to (loose) | "5" == 5 |
true |
!= |
not equal to (loose) | 5 != 5 |
false |
> |
greater than | 10 > 7 |
true |
< |
less than | 3 < 1 |
false |
>= |
greater than or equal to | 8 >= 8 |
true |
<= |
less than or equal to | 4 <= 2 |
false |
The strict operators (=== and
!==) are generally preferred over the
loose ones (== and !=)
because they compare both value and type, helping you avoid
subtle bugs.
For the reorder decision, the operator that matters is
<=:
let onHand = 140
if (onHand <= 150) {
console.log("Place an order")
} else {
console.log("No action needed")
}
The choice of <= rather than < is not
cosmetic: at exactly the reorder point you still want to order, and
using the wrong one would quietly let an item sit at its trigger level
without reordering.
5.2.2 Logical Operators
Logical operators let you combine conditions or invert one. They’re essential when a decision depends on more than one thing.
5.2.2.1 AND
(&&)
The whole expression is true only if both conditions are true.
if (onHand <= 150 && supplierIsPreferred) {
console.log("Place an order with the preferred supplier")
}
5.2.2.2 OR (||)
The whole expression is true if either condition is true.
if (isStockedOut || isOverstocked) {
console.log("Flag this item for review")
}
5.2.2.3 NOT (!)
Flips a boolean value.
if (!supplierIsPreferred) {
console.log("Use the backup supplier")
}
Logical operators let you express more nuanced decisions, especially when working with real data from a spreadsheet.
5.2.3 Using Operators With Spreadsheet Data
Apps Script is often used to read values from cells and evaluate them. A common operations decision is sorting items into classes by how much money they tie up — an ABC classification. The idea, rooted in Pareto’s 80-20 rule, is to rank items by annual dollar volume (unit cost times annual demand) so the few high-value items get the closest watch and the many low-value ones get a lighter touch. This script reads an item’s annual dollar volume from A1 and writes its class into B1:
function classifyItem() {
let sheet = SpreadsheetApp.getActiveSheet()
let annualDollarVolume = sheet.getRange("A1").getValue()
let cell = sheet.getRange("B1")
if (annualDollarVolume >= 50000) {
cell.setValue("A")
} else if (annualDollarVolume >= 10000 && annualDollarVolume < 50000) {
cell.setValue("B")
} else {
cell.setValue("C")
}
}
Here comparison operators (>=, <) and
a logical operator (&&) work together to classify
the item. This is the simple, threshold-based version of ABC; the more
rigorous method — sorting the whole catalog and walking down the
cumulative dollar volume until you’ve covered 80%, then 95% — needs a
way to process every item at once, which the next chapters provide.
5.2.4 Why These Operators Matter
Comparison and logical operators are the backbone of decision-making. They let your scripts validate input, categorize data, check several conditions at once, control flow, and respond intelligently to spreadsheet values. Once you’re comfortable with them, you can write much more expressive logic — tools that adapt to whatever data they encounter.
5.3 Real-World Examples of Decision-Making in Apps Script
Let’s bring the pieces together with the two decisions the University Store makes most often: when to reorder an item, and how much attention each item deserves.
5.3.1 Deciding When to Reorder
A good reorder rule doesn’t look at on-hand stock alone — it looks at the inventory position: what’s on hand, plus what’s already on order, minus what’s owed to back-orders. Tracking the position keeps you from ordering twice while a shipment is already on the way. When the position drops to the reorder point or below, you place an order — for the EOQ of 479 we computed in the last chapter.
function reorderDecision() {
let sheet = SpreadsheetApp.getActiveSheet()
let onHand = sheet.getRange("B2").getValue()
let onOrder = sheet.getRange("C2").getValue()
let backorders = sheet.getRange("D2").getValue()
let inventoryPosition = onHand + onOrder - backorders
let reorderPoint = 150
let orderQuantity = 479
if (inventoryPosition <= reorderPoint) {
sheet.getRange("E2").setValue("ORDER " + orderQuantity)
} else {
sheet.getRange("E2").setValue("HOLD")
}
}
Two details carry the whole policy. The comparison is
<=, not <, so an item resting exactly at
its reorder point still triggers an order. And the decision is made on
inventory position, not on-hand, so an item that’s low on the shelf but
has a delivery already inbound won’t set off a second, unnecessary
order.
5.3.2 Giving Each Item the Right Attention
Not every item deserves the same scrutiny. ABC classification ranks items by annual dollar volume so the handful that tie up most of the money — the A items — get the closest watch, while the many low-value C items get a lighter touch. In a typical catalog the A items are roughly a fifth of the products but the bulk of the value, and the C items are about half the products but a small slice of it. You can let the class drive the policy so attention follows value:
function reviewPolicy() {
let sheet = SpreadsheetApp.getActiveSheet()
let itemClass = sheet.getRange("B2").getValue()
let inventoryPosition = sheet.getRange("C2").getValue()
let reorderPoint = sheet.getRange("D2").getValue()
let cell = sheet.getRange("E2")
if (itemClass === "A" && inventoryPosition <= reorderPoint) {
cell.setValue("Order now and review weekly")
} else if (itemClass === "C") {
cell.setValue("Simple min-max; review quarterly")
} else {
cell.setValue("Standard review")
}
}
Here an equality (===), a comparison
(<=), and a logical AND (&&) work
together to turn a value class into an operating policy — exactly the
kind of judgment a buyer makes by hand, now written down so it runs the
same way every time.
5.3.3 From One Item to the Whole Catalog
Each function here decides about a single item, but the University Store stocks hundreds. To run the same reorder check across every row of a catalog — and to compute ABC the rigorous way, by sorting the whole list and walking down it until the cumulative dollar volume crosses 80% and then 95% — you need a way to hold many items at once and step through them one by one. That is exactly what arrays and loops give you, and it’s where the next two chapters go.