-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathtest_bool_arithmetic.ts
More file actions
75 lines (63 loc) · 1.73 KB
/
test_bool_arithmetic.ts
File metadata and controls
75 lines (63 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Test boolean values in arithmetic and coercion contexts
// Critical for static typing migration: booleans must coerce to 0/1 in arithmetic
// === Comparison results in arithmetic ===
let count = 0;
count += 1;
count += 1;
count += 1;
console.log(count); // 3
// === Boolean in ternary ===
const x = 5;
const y = 3;
const bigger = x > y ? "yes" : "no";
console.log(bigger); // yes
// === Boolean in if ===
if (10 > 5) {
console.log("10 > 5"); // 10 > 5
}
if (!(3 > 7)) {
console.log("3 not > 7"); // 3 not > 7
}
// === Boolean variables ===
const t: boolean = true;
const f: boolean = false;
console.log(t); // true
console.log(f); // false
// === Boolean in while loop ===
let i = 0;
let flag = true;
while (flag) {
i++;
if (i >= 3) flag = false;
}
console.log(i); // 3
// === Negation ===
console.log(!true); // false
console.log(!false); // true
console.log(!!0); // false
console.log(!!1); // true
console.log(!!""); // false
console.log(!!"hi"); // true
// === Boolean comparison chains ===
const a = 10;
const b = 20;
const c = 15;
console.log(a < c && c < b); // true
console.log(a > c || c > b); // false
// === Boolean coercion to string ===
const boolStr = "result: " + (5 > 3);
console.log(boolStr); // result: true
const boolStr2 = "negative: " + (5 < 3);
console.log(boolStr2); // negative: false
// === Equality operators returning booleans ===
console.log(1 === 1); // true
console.log(1 === 2); // false
console.log(1 !== 2); // true
console.log("a" === "a"); // true
console.log("a" === "b"); // false
console.log("a" !== "b"); // true
// === Boolean in array ===
const bools: boolean[] = [true, false, true];
console.log(bools[0]); // true
console.log(bools[1]); // false
console.log(bools.length); // 3