The report you’re building in this course answers questions like “How many delivery messages are there?” A number alone isn’t worth much: someone will ask which messages you counted and why one made the cut while another didn’t.
So you’ll start with one message and one question: Is this message about delivery? By the end of this lesson, you’ll write that question in JavaScript, store its yes-or-no answer, and explain why an exact comparison sometimes says no when you expected yes.
If you’re joining this course directly, you need to be able to read a record’s fields and change a supplied function’s return value. Read and Change Reporting Code introduces those skills. Here, you’ll move from formatting one message to deciding which messages belong in a report.
Read the short examples and predict their answers, then try your changes in the browser exercises. Console-style blocks show a conversation with JavaScript; you don’t need to open a Console or install anything. Write practice answers on paper or in a note before revealing them. The next exercise gives you an editor and Run Tests. Run often, read the feedback, and use Full screen for more space.
Meet One Message
Here’s one invented customer message, written as a record like the ones you read in the first course:
const message = {
id: 7,
topic: "delivery",
status: "approved",
text: "My parcel arrived three days late.",
};
This sample uses text for the message wording (the previous course called that field message). Topics are now lowercase, and IDs are numbers so you can compare numbers with text. These are choices in this dataset, not automatic changes JavaScript makes. The new status field tells you whether a topic label has been reviewed.
The topic field holds a label someone already reviewed and assigned to this message. The status field says that label was approved. You can read each field the way you did before, with message.topic or message.id.
Notice the quotes. The values "delivery" and "approved" are strings, which means quoted text. The id is the number 7, without quotes. That difference will matter in a moment.
Explore the Function the Exercise Gives You
In the exercise after this lesson, the platform hands you this starter code:
export function isDeliveryMessage(message) {
return TODO;
}
Leave export as it is, since it lets the platform reach your function. TODO is the part you replace. Here’s all you need to know about the rest:
messagein the parentheses is the function’s parameter. It stands for one message record, like the one above.- The exercise platform calls the function many times, each time with a different message, so the same line gets checked against many records.
- The
returnline passes your answer back to the platform, which checks it. A passing run shows that the tests passed. A failing test shows what your function returned and what it should have returned.
Your job is to replace TODO with the question you ask each message.
Ask the Question With ===
The === operator is called strict equality. It compares the value on its left with the value on its right and answers whether they’re the same. Here it is on the record from above.
In the sessions below, each line after a > or ... is code, and output without either prefix is JavaScript’s answer. After a const line, the answer is undefined: that line names a value and has nothing to report.
> const message = {
… id: 7,
… topic: "delivery",
… status: "approved",
… };
undefined
> message.topic === "delivery"
true
> message.topic === "billing"
false
The answers true and false are Boolean values. There are exactly two of them, spelled in lowercase, and each one is a complete yes-or-no answer. A Boolean doesn’t print anything or make a decision. It just states the answer.
You can keep that answer under a name with const, just as you named values in the first course:
> const message = { topic: "delivery" };
undefined
> const isDelivery = message.topic === "delivery";
undefined
> isDelivery
true
JavaScript works out the comparison first, then gives its answer the name isDelivery. A name that starts with is reads like the question it answers, which helps anyone checking your report later.
An AI assistant could write this line for you, and it would probably be right. Knowing exactly what it compares lets you say whether it asks the question you meant.
Your Turn: Store the Answer
Now write the body of the supplied function yourself. Inside the curly braces, store the comparison’s result in a const named isDelivery, then return isDelivery:
function isDeliveryMessage(message) {
// Your two lines go here.
}
In the exercise, you can replace TODO with the comparison alone, or replace the whole return TODO; line with your two lines.
Here’s the completed function:
function isDeliveryMessage(message) {
const isDelivery = message.topic === "delivery";
return isDelivery;
}
The comparison runs first, the const line stores its Boolean, and return hands that stored answer to the platform.
Your teammate now wants billing messages instead. Change your two lines so they ask about the topic "billing", with a name that matches the new question.
const isBilling = message.topic === "billing";
return isBilling;
Only the quoted topic and the name changed. In the next exercise, you’ll write questions like these yourself, including one that works for billing or any other topic.
When the Answer Is a Surprising No
A teammate reads your report and asks, “Is message 7 one of the delivery messages?” Before you check the topic, you want to make sure you’re looking at the right message. Here’s a line that looks reasonable:
const message = { id: 7, topic: "delivery" };
const isMessageSeven = message.id === "7";
Does isMessageSeven hold true or false? Decide before you open the answer.
> const message = { id: 7, topic: "delivery" };
undefined
> const isMessageSeven = message.id === "7";
undefined
> isMessageSeven
false
If you said true, you’re in good company. The message’s id is 7, so why does JavaScript say no?
Look at the quotes again. message.id holds the number 7, while "7" is a string: a piece of text that happens to show the digit 7. Strict equality answers true only when both sides have the same type and the same value. A number and a string never match, however alike they look on the screen.
Compare the number with a number, and you get the answer you wanted:
> const message = { id: 7, topic: "delivery" };
undefined
> message.id === 7
true
The same rule applies to Booleans. The value true and the string "true" have different types, so they’re never strictly equal:
> const isDelivery = true;
undefined
> isDelivery === "true"
false
> isDelivery === true
true
A type mismatch is quiet: no error message, just a false that could drop a message from your count unnoticed. When a comparison says no and you expected yes, check whether both sides are the same type.
Compare Strings Exactly
Strings follow the same strict rule, all the way down to each character. Capital letters count, and so do spaces:
> const message = { topic: "delivery" };
undefined
> message.topic === "Delivery"
false
> message.topic === "delivery "
false
To JavaScript, "Delivery" and "delivery " differ from "delivery". That’s why this course’s labels come from a fixed, reviewed list: an exact comparison only matches labels spelled the same way.
Sometimes the question you want to ask is the opposite one. The !== operator asks Is this different? and answers true when the two sides don’t match:
> const message = { status: "approved" };
undefined
> message.status !== "approved"
false
> message.status !== "pending"
true
Note: A single = doesn’t ask anything. It gives a name a value, as in const isDelivery = ..., so use three equals signs, ===, whenever you mean Is this the same?
Check Your Understanding
Here’s a second message. Decide what each of the three comparisons below gives back, and why, before you open the answer:
const message = {
id: 12,
topic: "Delivery",
status: "pending",
};
message.id === 12
message.topic === "delivery"
message.status !== "approved"
Here are the results of all three comparisons:
> const message = {
… id: 12,
… topic: "Delivery",
… status: "pending",
… };
undefined
> message.id === 12
true
> message.topic === "delivery"
false
> message.status !== "approved"
true
The id is a number compared with a number. The topic has a capital D, so it isn’t exactly "delivery". And "pending" is different from "approved", which is exactly what !== asks about.
Next up is the exercise Is This a Delivery Message?, where you’ll write the question line inside the supplied function yourself, and the platform will check it against messages you haven’t seen yet.