Bird
Raised Fist0
Typescriptprogramming~20 mins

Boolean type behavior in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following is the correct way to declare a boolean variable in TypeScript?
easy
A. let isDone: Boolean = 'true';
B. let isDone: bool = true;
C. let isDone = Boolean;
D. let isDone: boolean = true;

Solution

  1. Step 1: Understand TypeScript boolean type

    TypeScript uses the lowercase boolean type to declare true/false values.
  2. Step 2: Check each option's syntax

    let isDone: boolean = true; uses boolean with a true value correctly. let isDone: bool = true; uses bool which is invalid. let isDone = Boolean; assigns the Boolean constructor, not a value. let isDone: Boolean = 'true'; uses uppercase Boolean and a string, which is incorrect.
  3. Final Answer:

    let isDone: boolean = true; -> Option D
  4. Quick Check:

    TypeScript boolean type = let var: boolean = true [OK]
Hint: Use lowercase 'boolean' type for true/false in TypeScript [OK]
Common Mistakes:
  • Using 'bool' instead of 'boolean'
  • Assigning string 'true' instead of boolean true
  • Using uppercase 'Boolean' type incorrectly
2. Which of the following is the correct syntax to assign a boolean value in TypeScript?
easy
A. let flag: boolean = false;
B. let flag: boolean = 'false';
C. let flag: boolean = 0;
D. let flag: boolean = True;

Solution

  1. Step 1: Identify valid boolean values

    Boolean values in TypeScript are only true or false without quotes and lowercase.
  2. Step 2: Evaluate each option

    let flag: boolean = false; correctly assigns false. let flag: boolean = 'false'; uses a string 'false' which is invalid. let flag: boolean = 0; uses number 0 which is not boolean. let flag: boolean = True; uses capitalized True which is invalid in TypeScript.
  3. Final Answer:

    let flag: boolean = false; -> Option A
  4. Quick Check:

    Boolean values are true or false without quotes [OK]
Hint: Assign boolean values as true or false without quotes [OK]
Common Mistakes:
  • Using strings 'true' or 'false' instead of boolean literals
  • Using numbers like 0 or 1 as booleans
  • Capitalizing true or false
3. What will be the output of the following TypeScript code?
let a: boolean = true;
let b: boolean = false;
console.log(a && b);
medium
A. true
B. false
C. undefined
D. TypeError

Solution

  1. Step 1: Understand boolean AND operation

    The && operator returns true only if both operands are true.
  2. Step 2: Evaluate the operands

    Variable a is true, b is false, so true && false is false.
  3. Final Answer:

    false -> Option B
  4. Quick Check:

    true AND false = false [OK]
Hint: AND returns true only if both are true [OK]
Common Mistakes:
  • Assuming true && false is true
  • Confusing && with || operator
  • Expecting runtime error for boolean operations
4. Identify the error in the following TypeScript code:
let isActive: boolean = True;
console.log(isActive);
medium
A. No error, prints true
B. TypeError: Cannot assign True to boolean
C. SyntaxError: True is not defined
D. Runtime error: invalid boolean

Solution

  1. Step 1: Check boolean literal case sensitivity

    In TypeScript, boolean literals are lowercase true and false. True with uppercase T is undefined.
  2. Step 2: Understand error type

    Using True causes a ReferenceError because it is not declared or recognized as a boolean.
  3. Final Answer:

    SyntaxError: True is not defined -> Option C
  4. Quick Check:

    Boolean literals must be lowercase true or false [OK]
Hint: Boolean literals are lowercase true/false only [OK]
Common Mistakes:
  • Using uppercase True or False
  • Expecting TypeScript to auto-correct case
  • Confusing boolean literals with strings
5. Consider the following TypeScript code snippet:
function checkValue(val: any): boolean {
return !!val;
}
console.log(checkValue(0));
console.log(checkValue('hello'));
console.log(checkValue(null));

What will be the output of this code?
hard
A. false true false
B. true true false
C. false false true
D. true false true

Solution

  1. Step 1: Understand double negation (!!) in TypeScript

    The !! converts any value to its boolean equivalent: falsy values become false, truthy become true.
  2. Step 2: Evaluate each input

    0 is falsy, so !!0 is false. 'hello' is a non-empty string (truthy), so !!'hello' is true. null is falsy, so !!null is false.
  3. Final Answer:

    false true false -> Option A
  4. Quick Check:

    !!value converts to boolean true/false correctly [OK]
Hint: Double negation (!!) converts any value to boolean [OK]
Common Mistakes:
  • Assuming 0 is true
  • Confusing !! with single !
  • Expecting null to be true
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard