TruthinessFalsiness (TR01)
1: function checkTruthyFalsy(value) {
2: if (value) { // Value is implicitly coerced to boolean
3: console.log(`${value} is truthy`);
4: } else {
5: console.log(`${value} is falsy`);
6: }
7: }
8: checkTruthyFalsy(42); // Output: 42 is truthy (numbers other than 0 are truthy).
9: checkTruthyFalsy("hello"); // Output: "hello" is truthy (non-empty strings are truthy).
10: checkTruthyFalsy([]); // Output: [] is truthy (empty array is truthy, but has length zero, what is falsy).
11: checkTruthyFalsy({}); // Output: {} is truthy (empty object is truthy, it's not null, undefined, 0, false, '', or NaN).
12: checkTruthyFalsy(true); // Output: true is truthy (boolean true).
13:
14: checkTruthyFalsy(0); // Output: 0 is falsy
15: checkTruthyFalsy(""); // Output: "" is falsy (empty string is falsy).
16: checkTruthyFalsy(null); // Output: null is falsy
17: checkTruthyFalsy(undefined); // Output: undefined is falsy
18: checkTruthyFalsy(NaN); // Output: NaN is falsy
19: checkTruthyFalsy(false); // Output: false is falsy (boolean false).
Conditional Statements (if/else) hello is truthy is truthy [object Object] is truthy true is truthy 0 is falsy is falsy null is falsy undefined is falsy NaN is falsy false is falsy
TruthinessFalsiness context:
ES6 context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/JavascriptES6/TR01.htm
|
|