-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-truthy-falsy-values.js
61 lines (47 loc) · 1.3 KB
/
01-truthy-falsy-values.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
/*
There are values that when used in certain contexts, JavaScript
interprets as boolean.
For example, if we put a string inside the parenthesis of an if condition,
javascript will use it as a boolean:
- an empty string will mean false
- any other string will mean true
Falsy values:
"", 0, 0n, -0, null, undefined, NaN
Truthy values:
every value that is not falsy
For example: 1, 2, 3, -1, -2, "hello", "123"
*/
/*
Let's imagine we have a website where users can sign up with their email,
a password and optionally they could choose a username.
If they choose a username, the website greets them with "Hello, <username>!"
If they don't choose a username, the website will simply greet them with "Hello, dear guest"
*/
let username = "";
if (username) {
console.log(`Hello, ${username}`);
} else {
console.log("Hello, dear guest");
}
if (!username) {
console.log("Hello, dear guest");
} else {
console.log(`Hello, ${username}`);
}
/*
The same examples as above, but without relying on truthy and falsy values
*/
if (username !== "") {
console.log(`Hello, ${username}`);
} else {
console.log("Hello, dear guest");
}
if (username === "") {
console.log("Hello, dear guest");
} else {
console.log(`Hello, ${username}`);
}
let firstName = null;
if (!firstName) {
console.log("Plese, enter a valid username.")
}