0
0
Typescriptprogramming~20 mins

Boolean type behavior in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boolean Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Boolean conversion of different values
What is the output of this TypeScript code?
Typescript
const values = [0, 1, "", "hello", null, undefined, [], {}];
const results = values.map(v => Boolean(v));
console.log(results);
A[false, true, false, true, false, false, true, true]
B[false, true, true, true, false, false, false, true]
C[false, true, false, true, true, false, true, false]
D[true, true, false, true, false, true, true, true]
Attempts:
2 left
💡 Hint
Remember that Boolean() converts values to true or false based on their truthiness.
Predict Output
intermediate
2:00remaining
Boolean logic with && and || operators
What is the output of this TypeScript code?
Typescript
const a = 0;
const b = "hello";
const c = null;
console.log(a && b);
console.log(b || c);
A
0
hello
B
hello
null
C
false
hello
D
0
null
Attempts:
2 left
💡 Hint
Remember how && returns the first falsy or last truthy value, and || returns the first truthy or last falsy value.
🔧 Debug
advanced
2:00remaining
Unexpected Boolean result in conditional
Why does this TypeScript code print "No" instead of "Yes"?
Typescript
const value: any = "false";
if (value) {
  console.log("Yes");
} else {
  console.log("No");
}
ABecause the variable is typed as any, it causes a runtime error and prints "No".
BBecause the string "false" is falsy, so the else block runs and prints "No".
CBecause the string "false" is truthy, but the code prints "No" due to a syntax error.
DBecause the string "false" is truthy, so the if block runs and prints "Yes".
Attempts:
2 left
💡 Hint
Check how non-empty strings behave in Boolean context.
📝 Syntax
advanced
2:00remaining
Boolean type assignment error
Which option causes a TypeScript type error?
Typescript
let flag: boolean;
flag = ???;
Atrue
B"true"
Cfalse
DBoolean(0)
Attempts:
2 left
💡 Hint
Check which values are allowed for a variable typed as boolean.
🚀 Application
expert
2:00remaining
Count truthy values in an array
Given this TypeScript function, what is the output when called with the array [0, "", 1, "hello", null, undefined, [], {}]?
Typescript
function countTruthy(arr: any[]): number {
  return arr.reduce((count, item) => count + (item ? 1 : 0), 0);
}
console.log(countTruthy([0, "", 1, "hello", null, undefined, [], {}]));
A5
B6
C4
D7
Attempts:
2 left
💡 Hint
Count how many items are truthy in the array.