Strings, Numbers and Booleans
Strings represent text, numbers represent numeric values, and booleans represent true or false. A value’s type determines which operations make sense.
An article title is text, its reading time is a number, and its saved flag is a boolean.
See the mechanism
"Quiet mornings"4falseFollow one example
The quotes make the title a string. The unquoted 4 is a number, so adding 1 gives 5. false is a boolean value, not the text "false". Each console.log prints one value; the variable names do not appear in the output.
Predict, change and check
Predict: What changes if minutes is "4" instead of 4? Predict the result of minutes + 1 before running it.
Test your explanation: Change only minutes to "4". You should see 41: + joins text when a string is involved. Restore 4 and confirm 5 returns.
The tasks below ask you to explain that result and apply the idea to different inputs.
Apply and distinguish
A type tells you which question you are answering
Keep a title as a string, a computed reading time as a number, and a saved flag as a boolean. The string 'false' is nonempty and therefore truthy; it is not the boolean false. A DOM input's text value is a string even when its visible characters look numeric.
An operator can expose a mistaken assumption: '4' + 1 joins text, while 4 + 1 performs numeric addition. Write down the expected input type before fixing the expression. Do not convert every value to a boolean to 'clean' a record; zero and empty text can be valid values whose information would be lost.
Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.