-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefinitions.txt
195 lines (148 loc) · 7.91 KB
/
Definitions.txt
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
Variables : Variables are placeholders to store infomation or data. Variables can hold different data types like numebrs, strings, booleans and more.
Operators : Operators are used to assign values, compare values, perform arithmatic operations
prompt() : prompt() is the built-in JS function that helps you get input from user through the browser
alert() : alert() is like prompt(), but it only for output
Object : Object in JS are a type of variable, quite similar to arrays but they have something called key-value pairs. You can also store functions in Object list.
ex. -
const person = {
'name':'Omkar',
'age','26',
}
Accessing an object from object list : There are 2 ways to access an object (dot notation & bracket notation)
person.name; ----> dot notation
person['age']; ----> bracket notation
assigning an object :
person.phone = '123456789';
person['phone'] = '123456789';
Math.floor() : this function is used to round of the decimal number (3.24 => 3)
Math.ceil() : this function is used to round of the decimal number (3.24 => 4)
Math.random() : this function is used get random decimal number from 0-1 (0.624367372)
Math.random()*4 : We can multiply Math.random() function with any number whithin which you want to get random number(in this case you will get number within 0-4. 4 exclusive)
let : let is used to define the variable (ex. - let name = 'omkar')
const : const is used to define never changing value (ex. - const NAME = 'omakr')
function : function is the keyword used to define the function/method (syntax - function greeting(){}). A JS function is the block of code designed to perform a perticular task and is executed when something calls it.
function arguments : function arguments are the values received by the function when it is called, they behave as a variables however they only live within a function and cannot be accessed outside. (syntax - function sayMyName(name){})
Template literals `${variable name}` : Template literals provide an easy way to interpolet variables and expressions into strings. Interpolation is an easy/efficient way of concatination.
return : the return keyword/statement stopes the execution of a function and return a value.
Arrow functions : Arraow functions allows us to write shorter function syntax.
ex. -
Regular function -
function myFunction(name){
console.log(`hi S{name}`);
}
Arrow function with explicit return-
const myFunction = (name) => {
console.log(`hi S{name}`);
}
Arrow function with implicit return -
const sumArrow = (a,b) => a+b;
It basically tells us that we have a function called "sumArrow" which takes 2 arguments and return sum of them
Array syntax => let array = ['Banana','Orange','Apple','Pear']; or let array = [];
Adding element in array => array.push('pineapple'); - adds the element in the end
removing the element from array => array.remove(); - removes element at the end in array
removing the element at 1st position in array => array.
Getting lenght of array => array.length;
push() : this method adds the given element at the end of the array
pop() : this method removes 1st element from the array
shift() : this method removes 1st element from the array
unshift(element) : this method adds the given element at the start of the array
slice(); : The slice method return the position of the array you want in a new array (array.slice(0,2);)
indexOf('element'); : This method will give you the index of perticular element in array
this keyword : this keyword is used to refrence the object or variable in the current block
ex. -
const introducer = (assets, liabilities) => {
let person = {
assets : assets,
liabilities : liabilities,
netWorth : function(){
return this.assets-this.liabilities;
}
}
const intro = `Hi, My net worth is $${person.netWorth()}`;
return intro;
}
console.log(introducer(100000, 500));
for loop new syntax : in below for loop ({let variableName} of {arrayName})
let fruits = ['banana','mango','apple','pineapple'];
for(let i of fruits){
console.log(i);
}
map() : map() is the higher order function. it is the easiest way to loop through an array and returns an array (syntax : [1,2,3,4].map(number => console.log(number)))
filter() : filter() is the higher order function. it is the easiest way to loop through an array and returns the filtered array with matching conditions (syntax : [1,2,3,4].filter(number => number > 2)
reduce() : reduce() takes in an argument as a function. reduce() loop through an array and gives you an accumulator
API : API stands for Application Programming Interface and it let's your app communicate with other services to fetch the data.
================================================================================================================================================
Promises in JS : Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code.
Benefits of Promises
========================
Improves Code Readability
Better handling of asynchronous operations
Better flow of control definition in asynchronous logic
Better Error Handling
A Promise has four states:
========================
fulfilled: Action related to the promise succeeded
rejected: Action related to the promise failed
pending: Promise is still pending i.e. not fulfilled or rejected yet
settled: Promise has fulfilled or rejected
A promise can be created using Promise constructor.
Syntax :
let promise = new Promise(function(resolve, reject){
//do something
});
Parameters of promise
========================
Promise constructor takes only one argument which is a callback function (and that callback function is also referred as anonymous function too).
Callback function takes two arguments, resolve and reject
Perform operations inside the callback function and if everything went well then call resolve.
If desired operations do not go well then call reject.
You can use .then() and .catch() functions to process accourding to resolve or reject
You can also write promises using asyn/await
Rules for using async/await
========================
1. You must create a function
2. Always wrap function body into try catch block so you can later print or use error part also
3. You must use the keyword async
4. You must use the keyword await
5. Call the function
Example :
const promis1Data = async () => {
try{
const data = await promise1;
console.log(data); //Prints success/resolve message
}catch(error){//pass the error/reject message
console.log(error);//prints the error/reject message
}
}
promis1Data();
Ther are only 2 ways to extract the value out of the promise
1. You await the promise like below example
const sum = async (a,b) => {
const result = await a+b;
console.log(result);
}
console.log(sum(1,2));
2. You ".then(value => console.log(value));" the function like below example
const sum = async (a,b) => a+b
console.log(sum(1,2).then(value => console.log(value)));
================================================================================================================================================
.addEventListner() : It is a method which we can attach to any dom element so that our code will run accounrding to the everything
Event listner it takes 2 arguments 1st is event and 2nd is function. Below are the 2 examples of it
===========================
//Example 1
const click = document.getElementById('click');
click.addEventListener('click',() => console.log('hey hey'))
//Example 2
document.addEventListener('keydown', () => console.log('key pressed on keyboard'))
//Example 3
document.addEventListener('keydown', (e) => {
if(e.key == "w"){
console.log('You pressed Q');
}else if(e.key == "a"){
console.log('You pressed A');
}else if(e.key == "s"){
console.log('You pressed S');
}else if(e.key == "d"){
console.log('You pressed D');
}
})