What are the two possible values of a Boolean type in TypeScript?
The Boolean type in TypeScript can only have two values: true or false.
Click to reveal answer
intermediate
How does TypeScript treat non-Boolean values in a Boolean context?
TypeScript uses JavaScript's truthy and falsy rules. Values like 0, '' (empty string), null, undefined, and NaN are treated as false. Others like non-empty strings or numbers (except 0) are true.
Click to reveal answer
beginner
What is the result of Boolean('hello') in TypeScript?
The result is true because non-empty strings are considered truthy.
Click to reveal answer
intermediate
Explain the difference between == and === when comparing Boolean values.
== compares values after type coercion, so 1 == true is true. === compares both value and type, so 1 === true is false. It's best to use === to avoid unexpected results.
Click to reveal answer
beginner
How can you explicitly convert a value to a Boolean in TypeScript?
You can use the Boolean() function or double negation !!value to convert any value to a Boolean.
Click to reveal answer
Which of the following values is considered falsy in TypeScript?
A{}
B'false'
C[]
D0
✗ Incorrect
0 is falsy. Non-empty strings ('false'), empty arrays ([]), and empty objects ({}) are truthy.
What does the expression !!'0' evaluate to?
Afalse
Btrue
C0
Dundefined
✗ Incorrect
The string '0' is non-empty, so it is truthy. Double negation converts it to true.
Which operator checks both value and type equality for Booleans?
A===
B!=
C==
D||
✗ Incorrect
=== checks both value and type equality.
What is the Boolean value of null in TypeScript?
Atrue
Bnull
Cfalse
Dundefined
✗ Incorrect
null is falsy, so its Boolean value is false.
How do you convert a number to a Boolean explicitly?
AUsing <code>Boolean()</code>
BUsing <code>Number()</code>
CUsing <code>String()</code>
DUsing <code>parseInt()</code>
✗ Incorrect
Use Boolean() to convert any value to a Boolean.
Describe how TypeScript treats different values when used in a Boolean context.
Think about which values become true or false when converted.
You got /4 concepts.
Explain the difference between == and === when comparing Boolean values in TypeScript.
Focus on how type affects equality.
You got /4 concepts.
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
Step 1: Understand TypeScript boolean type
TypeScript uses the lowercase boolean type to declare true/false values.
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.
Final Answer:
let isDone: boolean = true; -> Option D
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
Step 1: Identify valid boolean values
Boolean values in TypeScript are only true or false without quotes and lowercase.
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.
Final Answer:
let flag: boolean = false; -> Option A
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
Step 1: Understand boolean AND operation
The && operator returns true only if both operands are true.
Step 2: Evaluate the operands
Variable a is true, b is false, so true && false is false.
Final Answer:
false -> Option B
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
Step 1: Check boolean literal case sensitivity
In TypeScript, boolean literals are lowercase true and false. True with uppercase T is undefined.
Step 2: Understand error type
Using True causes a ReferenceError because it is not declared or recognized as a boolean.
Final Answer:
SyntaxError: True is not defined -> Option C
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: