-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.short_circuiting.js
More file actions
170 lines (159 loc) · 4.14 KB
/
Copy path6.short_circuiting.js
File metadata and controls
170 lines (159 loc) · 4.14 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//SHORT CIRCUITING
//In certain conditions the operator will return the first value and not look at the second value
//Short-circuiting means JavaScript stops evaluating an expression as soon as it knows the final result.
const data = [
{
id: 1,
title: "The Lord of the Rings",
publicationDate: "1954-07-29",
author: "J. R. R. Tolkien",
genres: [
"fantasy",
"high-fantasy",
"adventure",
"fiction",
"novels",
"literature",
],
hasMovieAdaptation: true,
pages: 1216,
translations: {
spanish: "El señor de los anillos",
chinese: "魔戒",
french: "Le Seigneur des anneaux",
},
reviews: {
goodreads: {
rating: 4.52,
ratingsCount: 630994,
reviewsCount: 13417,
},
librarything: {
rating: 4.53,
ratingsCount: 47166,
reviewsCount: 452,
},
},
},
{
id: 2,
title: "The Cyberiad",
publicationDate: "1965-01-01",
author: "Stanislaw Lem",
genres: [
"science fiction",
"humor",
"speculative fiction",
"short stories",
"fantasy",
],
hasMovieAdaptation: false,
pages: 295,
translations: {},
reviews: {
goodreads: {
rating: 4.16,
ratingsCount: 11663,
reviewsCount: 812,
},
librarything: {
rating: 4.13,
ratingsCount: 2434,
reviewsCount: 0,
},
},
},
{
id: 3,
title: "Dune",
publicationDate: "1965-01-01",
author: "Frank Herbert",
genres: ["science fiction", "novel", "adventure"],
hasMovieAdaptation: true,
pages: 658,
translations: {
spanish: "",
},
reviews: {
goodreads: {
rating: 4.25,
ratingsCount: 1142893,
reviewsCount: 49701,
},
},
},
{
id: 4,
title: "Harry Potter and the Philosopher's Stone",
publicationDate: "1997-06-26",
author: "J. K. Rowling",
genres: ["fantasy", "adventure"],
hasMovieAdaptation: true,
pages: 223,
translations: {
spanish: "Harry Potter y la piedra filosofal",
korean: "해리 포터와 마법사의 돌",
bengali: "হ্যারি পটার এন্ড দ্য ফিলোসফার্স স্টোন",
portuguese: "Harry Potter e a Pedra Filosofal",
},
reviews: {
goodreads: {
rating: 4.47,
ratingsCount: 8910059,
reviewsCount: 140625,
},
librarything: {
rating: 4.29,
ratingsCount: 120941,
reviewsCount: 1960,
},
},
},
{
id: 5,
title: "A Game of Thrones",
publicationDate: "1996-08-01",
author: "George R. R. Martin",
genres: ["fantasy", "high-fantasy", "novel", "fantasy fiction"],
hasMovieAdaptation: true,
pages: 835,
translations: {
korean: "왕좌의 게임",
polish: "Gra o tron",
portuguese: "A Guerra dos Tronos",
spanish: "Juego de tronos",
},
reviews: {
goodreads: {
rating: 4.44,
ratingsCount: 2295233,
reviewsCount: 59058,
},
librarything: {
rating: 4.36,
ratingsCount: 38358,
reviewsCount: 1095,
},
},
},
];
function getBooks(id){
return data.find((d) => d.id ==id);
}
const bookThree = getBooks(3)
const pages = bookThree.pages
const condition = pages > 1000 ? "over 1000 pages" : "less than 1000 pages";
// The && operator = short circuits when the first operand, when the firt value is false; Returns the first falsy value
console.log(true && "some string");
console.log(false && "some string");
//////falsy values = 0, '', undefined
console.log("jonas" && "some string")
console.log(0 && "some string")
// The || operator = short circuits when the first operand, when the first value is true; Returns the first truthy value
console.log(bookThree.translations.spanish);
const spanishTranslation = bookThree.translations.spanish || "NOT TRANSLATION";
spanishTranslation
//Nullish coalescing — Returns the first non-null/undefined value
console.log(null ?? "Default"); // "Default"
console.log(undefined ?? "Fallback"); // "Fallback"
console.log(0 ?? 100); // 0 (not nullish)