forked from faisal2410/js_basic_ostad_b3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_truthy_falsy.js
85 lines (56 loc) · 2.04 KB
/
10_truthy_falsy.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
74
75
76
77
78
79
80
// In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context, while a falsy value is a value that is considered false when evaluated in a Boolean context.This can be useful when writing conditional statements or checking for the existence of a value.
// Here are some examples of truthy and falsy values in JavaScript:
// Truthy values:
if ("hello") {
console.log("This is a truthy value");
}
if (42) {
console.log("This is a truthy value");
}
if (true) {
console.log("This is a truthy value");
}
if ({}) {
console.log("This is a truthy value");
}
if ([]) {
console.log("This is a truthy value");
}
// All of the above examples will output "This is a truthy value" because they are considered truthy values in JavaScript.
// Falsy values:
if (false) {
console.log("This is a falsy value");
}
if (0) {
console.log("This is a falsy value");
}
if (null) {
console.log("This is a falsy value");
}
if (undefined) {
console.log("This is a falsy value");
}
if (NaN) {
console.log("This is a falsy value");
}
// All of the above examples will not output anything because they are considered falsy values in JavaScript.
// It's important to note that the concept of truthy and falsy values can be used in a variety of contexts in JavaScript. For example, when checking if a value exists, you can use the shorthand if (value) to check if the value is truthy, or if (!value) to check if the value is falsy.
function checkTruthyFalsy(value) {
let result = "";
if (value) {
result = `${value} is a truthy value`;
} else {
result = `${value} is a falsy value`;
}
return result;
}
console.log(checkTruthyFalsy(10));
// Output: "10 is a truthy value"
console.log(checkTruthyFalsy("hello"));
// Output: "hello is a truthy value"
console.log(checkTruthyFalsy(undefined));
// Output: "undefined is a falsy value"
console.log(checkTruthyFalsy(null));
// Output: "null is a falsy value"
console.log(checkTruthyFalsy(0));
// Output: "0 is a falsy value"