-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataTypes.js
239 lines (179 loc) · 4.93 KB
/
dataTypes.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// === Primitive Data Types ===
// --- Boolean ---
const bool1 = true;
const bool2 = false;
// Generating a boolean value
let age = 20;
const isOver25 = age > 25;
// console.log(isOver25);
// --- Numbers ---
const num1 = 1;
const num2 = 1.1;
const num3 = 2.01 * 'x';
// --- Strings ---
const name1 = 'karl1'; // Single quotes (quotation marks)
const name2 = 'karl2'; // Double quotes
// Backticks
// - put JS expressions inside them
// - multiline
const name3 = `karl3
${name2}
more lines
and more`;
// String() (string constructor) converts other values to a string
const name4 = String(1.3); // '1.3'
// --- Undefined ---
// "undefined" indicates a lack of initialization
const un = undefined;
// let variables will be undefined to start
let un2;
// console.log(un2);
// Accessing object properties with incorrect key names
const obj = { x: 1 };
console.log(obj.xx); // undefined
// --- Null ---
// "null" indicates an intentional setting of "empty"
const n = null;
age = null; // Reset a value to "empty" intentionally
// === Non-Primitive Data Types ===
// --- Objects ---
const user = {
id: 1, // id property: unique identifier that shouldn't change
firstName: 'Karl', // Object property, consisting of a key: firstName, and a value: 'Karl'
lastName: 'Horky',
// Nested object
address: {
streetName: 'Wipplingerstraße',
streetNumber: 56,
},
};
// "Object property" may also be called "attribute" or "key-value pair"
// Accessing an object property
const firstName = user.firstName; // Dot notation
const firstName2 = user['firstName']; // Bracket notation
const streetNumber = user.address.streetNumber; // Dot notation with nested object
// const streetNumberWithError = user.addresss.streetNumber;
console.log(
firstName,
firstName2,
streetNumber,
// streetNumberWithError,
);
// Bracket notation Example 1
let dropDown = 'lastName'; // value coming from user on website
const dynamicValue = user[dropDown];
console.log(dynamicValue);
// Bracket notation Example 2
const fruit = { color: 'red' };
const paint = { color: 'blue' };
let dropDown2 = 'color';
const selectedFruitValue = fruit[dropDown2]; // 'red'
const selectedPaintValue = paint[dropDown2]; // 'blue'
// Modifying an object
user.lastName = 'xxx';
// console.log(user);
// --- Arrays ---
const arr = ['abc', 'def']; // Array of strings
console.log(arr);
// Array of objects
const fruits = [
{ id: 1, color: 'red' },
{ id: 2, color: 'green' },
{ id: 3, color: 'green' },
];
// Accessing values in an array
console.log(arr[1]);
// console.log(fruits[1]);
console.log(fruits[0].color);
// Array methods
// Many of these array methods:
// 1. loop over each item
// 2. provide that item to function
// .map() - modify existing array (returns array)
console.log(
arr.map((item) => {
return item + 'x';
}),
);
const modifiedFruits = fruits.map((fruit) => {
const modifiedFruit = { ...fruit }; // Copy the object
modifiedFruit.color = 'purple';
return modifiedFruit; // return the modified item
});
console.log(modifiedFruits);
console.log(fruits);
// .filter() - retrieving multiple items (returns array)
console.log(
fruits.filter((fruit) => {
const isCorrectFruit = fruit.color === 'green';
return isCorrectFruit; // return a boolean
}),
);
// .find() - retrieving one item (returns single item)
console.log(
fruits.find((fruit) => {
const isCorrectFruit = fruit.color === 'purple';
return isCorrectFruit; // return a boolean
}),
);
// .forEach() - run some code for each item
fruits.forEach((fruit) => {
console.log('fruit', fruit);
});
// Probably better to use for...const...of
for (const fruit of fruits) {
console.log('fruit', fruit);
}
// .push() - add element to an array
arr.push('ghi');
arr.push('ghi');
arr.push('xxx', 'zzz');
// console.log(arr);
// pop, shift, unshift - retrieve or modify array items
// slice, splice - modify the number of elements in the array
// Spread syntax
console.log([
// Insert all elements of array
...arr,
'xxx',
'yyy',
]);
// --- Functions ---
function simpleFunction() {
console.log('simpleFunction');
}
simpleFunction();
simpleFunction();
simpleFunction();
simpleFunction();
const arrowFunction = () => {
console.log('arrowFunction');
console.log('arrowFunction');
console.log('arrowFunction');
};
arrowFunction();
const arrowFunction2 = () => console.log('arrowFunction2');
arrowFunction2();
// Returning a value
function functionWithReturnValue0() {
return 0;
}
const returnValue0 = functionWithReturnValue0();
console.log(returnValue0);
const functionWithReturnValue1 = () => {
return 1;
};
const returnValue1 = functionWithReturnValue1();
console.log(returnValue1);
const functionWithReturnValue2 = () => 2; // implicit return
const returnValue2 = functionWithReturnValue2();
console.log(returnValue2);
// Parameters
function sum(a, b) {
const result = a + b;
return result;
}
const result1 = sum(1, 2);
console.log(result1);
const result2 = sum(100, 220);
console.log(result2);