-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascriptnotes
More file actions
220 lines (139 loc) · 4.17 KB
/
javascriptnotes
File metadata and controls
220 lines (139 loc) · 4.17 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
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
a) Temlate Literals
back ticks
b) Destructing objects
const player = {
name: 'lebron James',
club: 'LA Lakers',
address: {
city: 'Los Angeles'
}
};
const { name, club, address: { city} } = player
c) Destructuring arrays
let [ fname,mname, lname ] = ['Sriramya','Govind','Prathi']
console.log(fname,mname, lname)
d) Object Literal
e) for of loop
const family = [ 'Govind', 'Sri', Dhwani', Shourya' ]
const name = "Govind"
for ( const member of family ){
console.log(member)
}
for ( const char of name){
console.log(char);
}
Spread operator:
let contacts = ["Mary", "Joel", "Danny"];
let personalFriends = [ "David", ...contacts, "Lily" ];
contacts.push("John");
let person = {
name: "Adam",
age: 25,
city: "Manchester"
}
let employee = {
...person,
salary: 50000,
position: "Software Developer"
}
console.log(employee)
g) Rest operator
function add(...nums) {
console.log(nums);
}
add(4, 5, 7, 8, 12)
;
h) Arrow Functions
const eveningSnack = snack => `I am going to eat ${snack} for evening Snacks`;
console.log(eveningSnack("biscuits"));
*** snack is single parameter passed to function eveningSnack, single return value of the string.
Default parameter for the function:
const leadSinger = (artist = "someone") => {
console.log(`${artist} is the lead singer of Cold Play`);
}
leadSinger("Chris Martin");
leadSinger();
*** we assign a default value "someone" to the artist
const shopping = (food = "something") => `I am going to buy ${food} from the grocery shop`;
console.log(shopping("milk"));
console.log(shopping());
i) includes
/*
** includes() Challenge
* You want to make a chocolate cake
* And you have a list of ingredients represented with an array
* Using the JavaScript includes() function
* Check if you have the ingredient chocolate in your list of ingredients, and print into the console "We are going to make a chocolate cake" if you have it
* Else print into the console "We can't make a chocolate cake because we are missing the ingredient chocolate"
*/
const listIngredients = [ "flour", "sugar", "eggs", "butter" ];
if(listIngredients.includes("chocolate")){
console.log("We are going to make chocolate cake");
}
else{
console.log("We can't make choclate cake because we ae missing the ingredient");
}
j) let & const
const for primitive types is immutable, but for arryays and object - we can assign values in the arrary but not change the assignment diretly
k) import and export
data.js
export const add = (num1, num2) => num1+num2;
index.js
import { add } from './data.js';
const result = add(1,2);
console.log(result);
l) Classes
m) fetch
fetch('https://jsonplaceholder.typicode.com/comments/1')
.then(response => response.json())
.then(data => console.log(data))
fetch('https://jsonplaceholder.typicode.com/comments',{
method: 'POST',
body: JSON.stringify({
name: 'Test',
email: "testmail@test.com",
body: "Testing Fetch POST commands"
})
})
.then(response => response.json())
.then(data => console.log(data))
n)
const photos = [];
async function photoUpload() {
let uploadStatus = new Promise( (resolve, reject) => {
setTimeout( () => {
photos.push("Profile Pic");
resolve("Photo Uploaded")
}, 3000)
})
let result = uploadStatus;
console.log(result);
console.log(photos.length);
}
photoUpload();
o) Promise
const data = new Promise((resolve, reject) => {
const error = false;
if(error){
reject('500 Image not found')
}
else{
resolve({
name: "Govind",
age: "41",
country: "India"}
)
}
})
data.then((data) => console.log(data)).catch((error) => console.log(error));
const apiUrl = "https://api.chucknorris.io/jokes/random";
let jokePromise = fetch(apiUrl)
jokePromise
.then((response) => response.json()).then((data) => console.log(data))
const apiUrl = "https://api.chucknorris.io/jokes/random";
async function getJoke() {
const response = await fetch(apiUrl);
const data = await response.json();
console.log(data);
}
getJoke();