Equality narrowing
📖 Scenario: Imagine you are building a simple program that checks the status of a device. The device can be either "on" or "off". You want to write code that behaves differently depending on the device's status.
🎯 Goal: You will create a variable to hold the device status, then use equality narrowing to check if the device is "on" or "off" and print a message accordingly.
📋 What You'll Learn
Create a variable called
deviceStatus with the exact value "on" or "off".Create a variable called
isOn that checks if deviceStatus is equal to "on".Use an
if statement with deviceStatus === "on" to print "The device is ON".Use an
else block to print "The device is OFF".💡 Why This Matters
🌍 Real World
Checking device or system status is common in apps that control hardware or monitor conditions, like smart home devices or servers.
💼 Career
Understanding equality narrowing helps you write safer and clearer code that reacts correctly to different states or inputs.
Progress0 / 4 steps