Back to questions
JuniorTheoryCommon
What does the program print?
function show(count: number | null) {
if (count) {
return "yes";
}
return "no";
}
console.log(show(0), show(-1));no yes
- Expecting
if (count)to drop onlynull: a truthiness check drops every falsy value, and0is falsy, soshow(0)returns"no". - Expecting tsc to reject a number in a condition: a condition need not be a
boolean, so the zero bug shows up only at run time.
- How would you change the check so that
show(0)returns"yes"? - Which other values does
if (x)cut off along with0?