-
Notifications
You must be signed in to change notification settings - Fork 0
/
true-false.js
73 lines (54 loc) · 1.74 KB
/
true-false.js
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
// TRUE and FALSE
// In JS, value of 'true' is 1 and value
// of false is 0, real literals, not just
// true and false
// const sum1 = true + 5;
// const sum2 = 5 + false;
// console.log(sum1); // -> 6
// console.log(sum2); // -> 5
// A scenario where undefined acts as false.
// fetching login details from some
// database but fetching failed
const loginDetails = {};
/* accessing the login ID */
const loginID = loginDetails.id;
/* checking loginID */
if (loginID !== undefined)
console.log('Allow user to login.');
else
console.log('Authentication failed.'); // -> printed
// can also be written as
if (loginID)
console.log('Allow user to login.');
else
console.log('Authentication failed.');
// FALSY VALUES: values that evaluate to false
// 1. false
const condition = false;
condition ? console.log('evaluates true.') : console.log('evaluates flase');
// -> evaluates flase
// 2. 0
const num = 0;
num ? console.log('evaluates true.') : console.log('evaluates flase');
// -> evaluates flase
// 3. empty string ''
const str = '';
str ? console.log('evaluates true.') : console.log('evaluates flase');
// -> evaluates flase
// 4. undefined
const someVar1 = undefined;
someVar1 ? console.log('evaluates true.') : console.log('evaluates flase');
// -> evaluates flase
// 5. null
const someVar2 = null;
someVar2 ? console.log('evaluates true.') : console.log('evaluates flase');
// -> evaluates flase
// TRUTHY VALUES
// Apart from the above given falsy values everything else
// evaluates to true even empty arrays and empty objects!
const arr = [];
arr? console.log('evaluates true.') : console.log('evaluates flase');
// -> evaluates true
const obj = {};
obj? console.log('evaluates true.') : console.log('evaluates flase');
// -> evaluates true