-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice.js
371 lines (263 loc) · 6.98 KB
/
practice.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var myDate =new Date()
// console.log(myDate)
// console.log(myDate.toString())
// console.log(myDate.toTimeString())
// console.log(myDate.toISOString());
// console.log(myDate.toLocaleString())
let myCreatedDate = new Date(2023,7,23)
// console.log(myCreatedDate.toDateString())
// console.log(myCreatedDate.toLocaleString())
let myTimeStamp=Date.now()
// console.log(myTimeStamp)
// console.log(Math.round(myTimeStamp/1000))
// console.log(`Hello World here are the number of seconds ${myTimeStamp} no these are miniseconds`)
/*************************************************/
//Arrays
const myArr = [0,1,2,3,4,5,6]
// console.log("A",myArr)
// console.log(myArr.includes(0))
// console.log(myArr.indexOf(5))
// var newArr = myArr.join()
// console.log(myArr)
// console.log(newArr)
// console.log( typeof newArr)
//slice splice
// var myn1=myArr.slice(1,3)
// console.log(myn1)
// console.log("B",myArr)
// var myn2=myArr.splice(1,3)
// console.log(myn2)
// console.log("c",myArr)
var marvelHero = ["thor","ironman","spiderman"]
var dcHero = ["superman","flash","batman"]
// marvelHero.push(dcHero)
// console.log(marvelHero).
var allHero = marvelHero.concat(dcHero)
// console.log(allHero)
var newAllHero = [...marvelHero,...dcHero]
// console.log(newAllHero)
const anotherArray=[1,3,5,[3,7,5],4,8,[3,5,[33,3,77]]]
const realAnotherArray = anotherArray.flat(Infinity)
// console.log(realAnotherArray)
// console.log(Array.isArray(realAnotherArray))
// console.log(Array.from("SAWAN"))
/************************** */
/*objects */
//singleton
//object.create
const mySym=Symbol("key1")
const newObj={
name:"sawan",
age:19,
[mySym]:"mykeyy1",
"full name":"SAWAN K",
location:"Delhi",
email:"[email protected]",
isLogedIn:false
}
// console.log(newObj.email)
// console.log(newObj["email"])
// console.log(newObj["full name"])
// console.log(newObj[mySym])
// console.log(typeof mySym)
newObj.email="[email protected]"
// Object.freeze(newObj)
// console.log(newObj)
newObj.greetings=function(){
console.log("hello print")
}
// console.log(newObj.greetings)//function return back
// console.log(newObj.greetings())
newObj.greetingsTwo=function(){
console.log(`hello ${this.name}`)
console.log(`Hello , ${this["full name"]}`)
}
// console.log(newObj.greetingsTwo())
const tinderuser = new Object() // singleton object
tinderuser.id="1234"
tinderuser.name="sawan"
tinderuser.isLoggedin=true
// console.log(tinderuser)
const regularUser = {
email:"[email protected]",
fullname:{
userFullname:{
firstname:"sawan",
lastname:"kumar"
}
}
}
// console.log(regularUser.fullname.userFullname)
const testobj1={1:"a",2:"b"}
const testobj2={3:"c",4:"d"}
const testobj3 = {...testobj1,...testobj2}
// const testobj3={testobj1,testobj2}
// const testobj3 = Object.assign({},testobj1,testobj2)
// console.log(testobj3)
// console.log(Object.keys(tinderuser))
// console.log(Object.values(tinderuser))
// console.log(Object.entries(tinderuser))
// console.log(tinderuser.hasOwnProperty("isLoggedin"))
/*apis and destructuring*/
const course = {
coursename:"js in hindi by hitesh sir",
price:"999",
courseinstructor:"Hitesh Choudhary"
}
// course["courseinstructor"]
const {courseinstructor: instructor} = course
// console.log(instructor)
const navbar=({company})=>{
} //destructuring
// navbar(company="sawan")
/* ************functions************ */
function add(num1,num2){
return num1+num2
}
const res=add(3,5)
// console.log(res)
function loginUserMessage(username="Meen"){
if(!username){
console.log("please enter a username")
return
}
return `${username} just logged in`
}
// console.log(loginUserMessage("Sawan"))
// console.log(loginUserMessage())
function calculateCartNumber(...num1){
return num1
}
// console.log(calculateCartNumber(200,3,223,44,332,0))
const user={
username:"sawan",
price:199
}
function handleObj(anyobject){
console.log(`username is ${anyobject.username} and price is ${anyobject.price}`)
}
// console.log(handleObj(user))
const newArr = [200,300,400]
function returnSecondIndex(givenArr){
return givenArr[1]
}
// console.log(returnSecondIndex(newArr))
/* scoping */
let a =30
if (true) {
let a=10
const b= 20
}
// console.log(a);
// console.log(b);
// console.log(c);
/* arrow function */
const newUser={
username:"sawan",
price:299,
welcomeMessage: function(){
console.log(`${this.username},welcome to website`)
}
}
// newUser.welcomeMessage()
newUser.username="sam"
// newUser.welcomeMessage()
// console.log(this)
// function chai(){
// let username = "sawan"
// console.log(this.username)
// }
// chai()
// const chai = function(){
// let username = "sawan"
// console.log(this.username)
// }
// chai()
// const chai = () => {
// let username = "saw"
// console.log(this.username)
// }
// chai()
// const addTwo = (num1,num2) => {
// return num1 + num2
// }
// console.log(addTwo(2,333333))
const addTwo = (num1,num2) => (num1+mum2)
const retObj = () => ({username:"sawan"});
// console.log(retObj())
/* Immediately Invoked Function Expressions (IIFE) */
// (function chai(){ //named IIFE
// console.log(`DB Connected`);
// })();
/*const n1 = new Object();
n1.id=123;
n1.name="newSawan";
n1.class="CSE"
((name)=>{ //Unnamed IIFE
console.log(`dB Connected ${name}`)
})(`${n1.name}`);*/
// (()=>(console.log("hello")))(); //IIFE
/* Control flow */
const isUserLoggedIn= true;
/*if (2==="2") {
console.log(`executed`)
} else{
console.log(`chomu`)
}
const score = 200
if (score > 100) {
const power = "fly"
console.log(`user power: ${power}`)
}*/
const balance = 1000
// if (balance > 500) console.log("test clear"),console.log("greater than 500");
if ( balance < 500 ) {
console.log("leass than 500")
} else if ( balance < 750 ) {
console.log("leass than 750")
} else if ( balance < 1200 ) {
//console.log("less than 1200");
}
const month = 3
switch (month) {
case 1:
console.log(`january`)
break;
case 2:
console.log(`feb`)
break
case 3:
//console.log(`March`)
break
case 4:
console.log(`april`)
break
default:
console.log(`default run`)
break;
}
/* Truthy and Falsy */
const userEmail = "[email protected]"
if(userEmail){
console.log("user email found");
} else{
console.log("not found");
}
//Falsy values
/* false, 0, -0, bigint 0n, "", null, undefined, NaN */
//Truthy values
/* not falsy values, "0", "false", " ", [], {}, function(){} */
const emptyobj = {}
if(Object.keys(emptyobj).length===0){
console.log("obj is empty")
}
// console.log(false == 0) --\
// console.log(false == '') |---> all are true
// console.log('' == 0) ----/
// && and operator || or operator
// Nullish coalescing operator (??): null and undefined
let val1;
// val1 = 5 ?? 10
// val1 = null ?? 10
// val1 = undefined ?? 15
console.log(val1)