🔍 What Type Is It?
🏷️
THE typeof OPERATOR
typeof "HAP" = "string"
Try these:
⚠️
THE TYPE TRAP: "5" + 1
With quotes (string)
"5" + 1
"51"
Without quotes (number)
5 + 1
6
Why? When you use
+ with a string, JavaScript converts the other value to a string too! The quotes make "5" a string, not a number. Use typeof
to check, and Number() to convert.
✨
TEMPLATE LITERALS
Old way (concatenation)
"Hello! I am " + robotName + ", created in " + creationYear + "."
Hello! I am HAP, created in 2024.
New way (template literal)
`Hello! I am ${robotName}, created in ${creationYear}.`
Hello! I am HAP, created in 2024.
✓✗
BOOLEANS VS "BOOLEANS"
Real boolean
true
typeof: "boolean" String that looks like boolean
"true"
typeof: "string" Watch out!
true (no quotes) is a boolean.
"true" (with quotes) is a string! They look similar but are completely different types. When in doubt, use typeof.
🟠 HAP's Tip: Types matter! "5" and 5 look the same but behave differently. Always use typeof when something unexpected happens—it saved me from a 20-minute debugging session!