Coersion (CR29)
1: const num = 42;
2: const str = String(num); // Number to string
3: const bool = true;
4: const nul = null;
5: const und = undefined;
6: const special = NaN;
7: const big = 1234567890123456789n;
8:
9: const boolFromNum = Boolean(num); // Number to boolean (0 is falsy, others truthy)
10: const boolFromStr = Boolean(str); // String to boolean (empty string "" is falsy, others truthy)
11: const boolFromBig = Boolean(big); // BigInt to boolean (0n is falsy, others truthy).
12: const boolFromNul = Boolean(nul); // Null to boolean (falsy)
13: const boolFromUnd = Boolean(und); // Undefined to boolean (falsy)
14: const boolFromNan = Boolean(special); // NaN to boolean (falsy)
15:
16: console.log(boolFromNum);
17: console.log(boolFromStr);
18: console.log(boolFromBig);
19: console.log(boolFromNul);
20: console.log(boolFromUnd);
21: console.log(boolFromNan);
Boolean Conversions: true true true false false false
Coersion context:
ES6 context:
Comments (
)

Link to this page:
http://www.vb-net.com/JavascriptES6/CR29.htm
|