-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
251 lines (206 loc) · 5.84 KB
/
functions.js
File metadata and controls
251 lines (206 loc) · 5.84 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// console.log("h");
// console.log("i");
// console.log("m");
// console.log("a");
// console.log("n");
// console.log("s");
// console.log("h");
// console.log("i");
// function declaration
function sayMyName() {
console.log("h");
console.log("i");
console.log("m");
console.log("a");
console.log("n");
console.log("s");
console.log("h");
console.log("i");
}
// sayMyName // function reference
// sayMyName() // function execution
//function defination mein jo input variables lete h wo parameters hote h
//jo function call pe values pass krte h wo arguements
function addno(num1, num2) {
console.log( num1 + num2)
}
// addno(3,4);//7 //function call pass arguements
// addno(3,"4"); //34
// addno(3,null);//3
function addNo(no1, no2) {
let result = no1 + no2;
return result
// return no1 +no2
}
// console.log(addNo(2,3));
const ans = addNo(5,4)
// console.log(ans)
//diff diff way to pass values
// function loginUserMessage(username) {
// return `${username} just logged in`;
// }
// loginUserMessage("himanshi")
//console.log(loginUserMessage("himanshi")) //print the returned value
// console.log(loginUserMessage(""))// empty String
// console.log(loginUserMessage()) // return undefined
function loginUserMessage(username) {
if(!username) {
console.log("pls enter user name");
return;
}
return `${username} just logged in`;
}
// loginUserMessage("himanshi")
// console.log(loginUserMessage())
// bydefault value bhi store kr skte h username = "sam" as parameter
//funtions with object and array
//use rest operator (...num)
function calculateCartPrice(...num){
return num ;
}
// console.log(calculateCartPrice(200,300,400,2000)) //output [200,300,400,2000]
//if we pass multiple values to perameter it will contain the first value
//to solve this problem we can use rest opetator(...num) it is diff from spread operator it stores all the value in an array
const user = {
username : "himanshi",
price : 199
}
function handleObject(anyobject){
console.log(`username is ${anyobject.username} and price is ${anyobject.price}`)
}
// handleObject({
// username: "himanshi",
// price : 199
// })
// handleObject(user)
// function with array
const myNewArray = [200,300,400,2000]
function returnSecondValue(getArray) {
// for(let key in getArray) {
// console.log(`price is ${getArray[key]} `);
// }
return getArray[2]
}
// returnSecondValue(myNewArray)
// console.log(returnSecondValue(myNewArray))
// block sope and global scope
// parent variables child access kr sakta h
function one() {
const username = "hitesh"
function Two() {
const website = "youtube";
console.log(username);
}
// console.log(website);
console.log("himasnhi");
Two()
}
// one()
if(true) {
const username = "himanshi"
if(username === "himanshi") {
const website = " youtube"
// console.log(username + website)
}
// console.log(website);
}
// console.log(username)
// +++++++++++++++++++++++++++ interesting
// two way of creating functions
// console.log(addone(5))
function addone(num) {
return num + 1
}
// if we call a function before declaration the function will execute
// addTwo(5)
const addTwo = function(num) {
return num + 2
}
// functions hoisting
// if we assign a function in a variable and call that function variable before function declaration it will give error
// this keyword refers to the current context with in a object
const user1 = {
username : "hitesh",
price : 999,
welcomeMessage : function() {
console.log(`${this.username}, welcome to website`)
console.log(this);
}
}
// user1.welcomeMessage()
// user1.username = "himanshi" //value change
// user1.welcomeMessage();
// console.log(this) // output empty object curly braces {}
//
// this keyword not in use in function
// function chai() {
// const username = "himanshi"
// console.log(this.username); // output undefined
// }
// chai()
// const chai = function() {
// const username = "himanshi"
// console.log(this.username);
// }
// chai()
// arrow function
const chai = () => {
const username = "himanshi"
console.log(this);
}
// chai()
// expleset return means using return keyword
// const add1 =(num1 , num2) => {
// return num1 + num2
// }
// impleset return (for single line statement)
// const add1 =(num1 , num2) => num1 + num2
// without using return keyword using parentheses
// const add1 =(num1 , num2) => (num1 + num2)
// console.log(add1(3,2))
// object return krne k liye curly braces me wrap krte h jo is way mein krnge
const add1 =(num1 , num2) => ({username: "himanshi"})
// console.log(add1(3,2))
const myArray = [2,46,5,7,54]
// classic function
myArray.forEach(function() {
})
// arrow function
myArray.forEach(() => {
})
// IIFE immediately invoked function expretions
// first parentheses and second parentheses()()
function chai1() {
console.log(`DB CONNECTED`);
}
// () //immediately invoke
// chai1()
// global scope k pollution se problem hoti h to uske variable declaration se backne k liye block use krte h parenthesis laga k
// to imediately invoke a funtion we can write a funtion inside parenthesis to create a block
// (function chai1() {
// // named iife bc function has name ie chai1
// console.log(`DB CONNECTED`);
// }) () ; // at the end use semi colon to end the line
// ( (name) => {
// // simple iife unnamed iife
// console.log(`db connected ${name}`)
// })("himanshi");
// js execution context + call stack
// {} // global EC
// last in first out LIFO concept
// function call inside function
// call stack //inside dev tool sources create new snipet to check the call stack
function one() {
console.log(`one`)
two()
}
function two() {
console.log(`two`)
three()
}
function three() {
console.log(`three`)
}
one()
two()
three()