Concept Flow - Boolean type behavior
Start
Evaluate expression
Is value truthy or falsy?
Yes→Value is true
No
Value is false
Use boolean in logic
End
This flow shows how a value is evaluated to true or false in TypeScript, then used in logic.
const a = 0; const b = Boolean(a); console.log(b);
| Step | Expression | Evaluated Value | Boolean Conversion | Output |
|---|---|---|---|---|
| 1 | a = 0 | 0 (number) | N/A | N/A |
| 2 | Boolean(a) | 0 | false | N/A |
| 3 | console.log(b) | b = false | false | false |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| a | undefined | 0 | 0 | 0 |
| b | undefined | undefined | false | false |
Boolean type behavior in TypeScript: - Values convert to true or false using Boolean(value). - Falsy values: 0, '', null, undefined, NaN, false. - Truthy values: most others. - Use Boolean() to explicitly convert. - Boolean values control logic flow and conditions.
boolean type to declare true/false values.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.true or false without quotes and lowercase.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.let a: boolean = true;
let b: boolean = false;
console.log(a && b);
&& operator returns true only if both operands are true.a is true, b is false, so true && false is false.let isActive: boolean = True;
console.log(isActive);
true and false. True with uppercase T is undefined.True causes a ReferenceError because it is not declared or recognized as a boolean.function checkValue(val: any): boolean {
return !!val;
}
console.log(checkValue(0));
console.log(checkValue('hello'));
console.log(checkValue(null));!! converts any value to its boolean equivalent: falsy values become false, truthy become true.Pass the test and this pattern moves unknown