const values = [0, 1, "", "hello", null, undefined, [], {}]; const results = values.map(v => Boolean(v)); console.log(results);
In JavaScript/TypeScript, Boolean() converts values to true or false based on truthiness. 0, empty string "", null, and undefined are false. Non-empty strings, arrays, and objects are true.
const a = 0; const b = "hello"; const c = null; console.log(a && b); console.log(b || c);
The && operator returns the first falsy value or the last value if all are truthy. Since a is 0 (falsy), a && b returns 0. The || operator returns the first truthy value or the last value if all are falsy. Since b is "hello" (truthy), b || c returns "hello".
const value: any = "false"; if (value) { console.log("Yes"); } else { console.log("No"); }
In JavaScript/TypeScript, any non-empty string is truthy, even if it contains the text "false". So the if (value) condition is true and prints "Yes". If the code prints "No", it means the code was not run as shown or there is a misunderstanding.
let flag: boolean;
flag = ???;TypeScript's boolean type only accepts true or false (or expressions that evaluate to boolean). The string "true" is not a boolean, so assigning it causes a type error.
function countTruthy(arr: any[]): number { return arr.reduce((count, item) => count + (item ? 1 : 0), 0); } console.log(countTruthy([0, "", 1, "hello", null, undefined, [], {}]));
The truthy values are: 1, "hello", [], and {}. That's 4. But check carefully: [] and {} are truthy, so total is 4. Wait, let's count carefully: 1, "hello", [], {} → 4 truthy values. So answer is 4.
Correction: The options show 4,5,6,7. The correct count is 4.