0
0
Typescriptprogramming~20 mins

Truthiness narrowing in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Truthiness Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of truthiness narrowing with && operator
What is the output of the following TypeScript code?
Typescript
function checkValue(val: string | null) {
  if (val && val.length > 3) {
    return val.toUpperCase();
  }
  return "Too short or null";
}

console.log(checkValue("test"));
console.log(checkValue("no"));
console.log(checkValue(null));
A"TEST"\n"Too short or null"\n"Too short or null"
B"TEST"\n"Too short or null"\n"null"
C"TEST"\n"no"\n"Too short or null"
D"test"\n"no"\n"null"
Attempts:
2 left
💡 Hint
Remember that && checks if the first value is truthy before evaluating the second.
Predict Output
intermediate
2:00remaining
Output of truthiness narrowing with if statement
What will be printed by this TypeScript code?
Typescript
function printLength(str?: string | null) {
  if (str) {
    console.log(str.length);
  } else {
    console.log("No string");
  }
}

printLength("");
printLength("hello");
printLength(null);
A0\nNo string\nNo string
BNo string\n5\nNo string
C0\n5\nNo string
DNo string\nNo string\nNo string
Attempts:
2 left
💡 Hint
Empty string is falsy in JavaScript/TypeScript.
🔧 Debug
advanced
2:00remaining
Identify the error in truthiness narrowing
What error will this TypeScript code produce when compiled or run?
Typescript
function getFirstChar(s: string | null) {
  if (s === null || s.length === 0) {
    return "Empty";
  }
  return s[0];
}

console.log(getFirstChar(null));
AReturns 'Empty' without error
BType error: s might be null when accessing s[0]
CSyntax error: missing colon
DRuntime error: Cannot read property 'length' of null
Attempts:
2 left
💡 Hint
Logical OR (||) short-circuits: if the left side is truthy, the right side is not evaluated.
📝 Syntax
advanced
2:00remaining
Which option correctly narrows a union type using truthiness?
Which of the following TypeScript snippets correctly narrows the variable 'input' of type string | undefined using truthiness?
Aif (input !== undefined && input.length > 0) { console.log(input); }
Bif (input.length > 0) { console.log(input); }
Cif (input && input.length > 0) { console.log(input); }
Dif (input != null || input.length > 0) { console.log(input); }
Attempts:
2 left
💡 Hint
Check which condition safely narrows input to a string before accessing length.
🚀 Application
expert
2:00remaining
Determine the output count of filtered array using truthiness narrowing
Given this TypeScript code, how many elements will be in the filtered array?
Typescript
const values: (string | null | undefined)[] = ["apple", "", null, "banana", undefined, " "];
const filtered = values.filter(v => v && v.trim().length > 0);
console.log(filtered.length);
A3
B5
C4
D2
Attempts:
2 left
💡 Hint
Remember that empty string and strings with only spaces behave differently with trim().