-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascriptBasic.js
More file actions
447 lines (303 loc) · 9.59 KB
/
Copy pathjavascriptBasic.js
File metadata and controls
447 lines (303 loc) · 9.59 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// 1 Deloitte
console.log(1 + false);
console.log(1 + true);
console.log(1 + null);
console.log(1 - false)
//1,2,1,1
const arr1 = [1, 2, 3];
const str = "1,2,3";
console.log(arr1 == str); true
console.log(arr1 === str); false
console.log("5" == 5) // true
console.log("5" === 5) // false
let x = 10;
{
let x = 20;
console.log(x); // 20
}
console.log(x); // 10
var y = 20;
{
var y = 30;
console.log(y); // 30
}
console.log(y); // 30
console.log(typeof null); // object
console.log(typeof undefined); // undefined
console.log(typeof NaN); // number
console.log(typeof 0); // number
console.log(typeof ""); // string
console.log(typeof []); // object
//-----------------------------------------------
console.log(NaN == NaN); // false
console.log(NaN === NaN); // false
console.log(NaN != NaN); // true
//--------------------------------------------
//----------
console.log(z); // undefined
var z = "hello"
console.log(z); // hello
console.log(10 + 2 * 3) // 16 high precedence *
console.log(10 - 2 * 3) // 4 high precedence *
console.log(10 / 2 * 3) // 15 high precedenc /
console.log(10 / 2 - 3) // 2
console.log(10 % 3) // 1
console.log([1, 2, 3] + [1, 3, 4])
// [1,2,3,1,3,4] array to string
// additon operater with two array javascript will convert to String
console.log([1, 2, 3] - [1, 3, 4])
// NaN array to string then string to number
console.log("a" - "b") // NaN
console.log("a" + "b") // ab
console.log("a" * "b") // NaN
console.log("a" / "b") // NaN
console.log("a" - 1) // NaN
console.log(+true); // 1
console.log(+false); // 0
console.log(+null); // 0
console.log(+undefined) // NaN
console.log(+[]); // 0
console.log(+NaN) // NaN
console.log(+0) // 0
console.log(+1); // 1
console.log(+"hello"); // NaN
console.log(!"hello"); // false
console.log(!""); // true
console.log(!0); // true
//------------
//------------------------
// when we try to add two arrays then javascript will convert arry to string using tyepcohersion
console.log([] + []) // ""
console.log([] + [1]) // "1"
console.log([1] + []) // "1"
console.log([1] + [2]) // "12"
console.log([1] + [2, 3]) // "123"
console.log([1, 2] + "abc") // "1,2abc"
//----------------------------
//------------------
function getAge(...args) {
console.log(arguments); // [40]
// arguments is an array-like object
// that contains the arguments passed to the function
console.log(args);[40]
console.log(typeof arguments); // object
console.log(typeof args); // object
console.log(arguments.length); // 1
console.log(args.length); // 1
}
getAge(40);
//--------------
//------------
// TCS
// in javascript when ever had duplicate property in object then last property will be taken
const obj = {
a: 'one',
b: 'two',
a: 'three'
}
console.log(obj) // { a: 'three', b: 'two' }
// when we have duplicate property in object then last property will be taken
var L = 1, T = L = typeof T;
console.log(L); // 1
console.log(T); // number
console.log(typeof L); // number
console.log(typeof T); // string
//------------------------
console.log('1' - - '1'); // Coerces strings to numbers and evaluates as 1 - (-1) = 2
//---------------------------
console.log(1 + 2 + "3"); // 33
console.log(false || null || "hello") // "hello" OR operatore will return the first truthy value
console.log(false && null && "hello") // false AND operatore will return the first falsy value
// OR operatore will return the first truthy value
// && operatore will return the first falsy value
//or // 1st false is falsy so it will go to next null which is also falsy so it will go to next "hello" which is truthy
//---------------
const numbers = [1, 2, 3, 4, 5];
const [x1, ...y1] = numbers;
console.log(x1); // 1
console.log(y1); // [2,3,4,5]
const str1 = "abc" + + "def";
// Concatenates "abc" with NaN because +"def" tries to convert "def" to a number, resulting in NaN
console.log(str1); // "abcNaN"
let newList = [1].push(2);
// push returns the new length (2),
// not the array, so newList is a number,
// causing errors in subsequent operations.
// push method will return the new length
// of the array after adding the element
// console.log(newList.push(3))
//------------------
// 1. The delete operator removes a property from an object.
let nums = [1, 2, 3, 4, 5];
delete nums[2]; // Deletes the element at index 2
console.log(nums); // [1, 2, <1 empty item>, 4, 5]
// to delete corret way is using splice meth
nums.splice(2, 1); // Removes the element at index 2
console.log(nums); // [1, 2, 4, 5]
//----------------
//-------
console.log(0 || 1) // 1 // 0 is falsy so it will go to next 1 which is truthy
console.log(1 || 0) // 1
console.log(0 || 0) // 0
console.log(1 || 2) // 1 first truthy will be returned
console.log(1 && 0) // 0 first falsy will be returned
console.log(0 && 1) // 0
console.log(1 && 2) //2 2 is last truthy value
console.log(1 && 0 && 2) // 0 first falsy will be returned
//-----
//--------
// postfix operatore
let numss = 0; // Initialize `numss` to 0
console.log(numss++); // 0
// Logs the current value of `numss` (0),
// then increments it by 1 (postfix increment).
console.log(numss); // 1
// Logs the updated value of `numss`,
// which is now 1 after the previous increment.
console.log(++numss); // 2
// Increments `numss` by 1
// first (prefix increment),
// making it 2, then logs the updated value.
console.log(numss); // 2
// Logs the current value of `numss`,
// which remains 2 after the previous increment.
console.log(numss--); // 2
// Logs the current value of `numss` (2),
// then decrements it by 1 (postfix decrement).
//------
console.log(5 < 8 > 2)
console.log(5 < 8 && 8 > 2) // true
console.log(5 < 8 && 8 > 2) // true
console.log(1 > 19 < 2) // true
//console.log(data()) // undefined
var data = function () {
console.log("hello");
}
console.log(data()) // hello
const arr = [1, 2, 3];
// Initializes an array with three elements: [1, 2, 3].
arr[5] = 9;
// Sets the value at index 5 to 9, leaving indices 3 and 4 as empty slots.
console.log(arr);
// Logs the array: [1, 2, 3, <2 empty items>, 9].
console.log(arr.length);
// Logs the length of the array: 6, as the highest index is 5 (0-based indexing).
const obj2 = { a: 1 };
obj2.a = 20;
console.log(obj2)
const obj5 = [10, 20, 30];
obj5[1] = 20;
console.log(obj5) // TypeError: Assignment to constant variable
const obj4 = { c: 1 };
obj4.c = 2;
console.log(obj4) // { c: 2 }
const obj3 = { c: 1 };
//obj3 = { d: 2 };
//console.log(obj3)
//
// // TypeError: Assignment to constant variable
let xyz = {
a: undefined,
b: null,
}
console.log(JSON.stringify(xyz)) // {"b":null}
console.log(JSON.parse(JSON.stringify(xyz))) // { b: null }
console.log(xyz) // { a: undefined, b: null }
const str5 = "hello";
str5.data = "world";
console.log(str5) // hello
console.log(str5.data) // undefined
const s1 = "hello";
const s2 = new String("hello");
console.log(s1 == s2); // true
console.log(typeof s1); // string
console.log(typeof s2); // object
console.log(s1 === s2); // false
console.log(Boolean({}))
console.log(Boolean(null)) // false
console.log(Boolean(undefined)) // false
console.log(Boolean([])) // true
console.log(Boolean([]))
console.log(Boolean(0))
console.log(Boolean(""))
console.log(Boolean(1)) // true
console.log(Boolean("hello")) // true
console.log(Boolean("0")) // true
console.log(Boolean("false")) // true
console.log(Boolean(" ")) // true
console.log(new Boolean(false)) // Boolean { false }
console.log(Boolean(new Boolean(false))) // true
//----------------
console.log(1 == "1") // true
console.log([] == []) // false
console.log([] == ![]) // true
// console.log([] === []) // false
console.log({} == {}) // false
console.log({} == !{}) // true
console.log({} == true) // false
console.log({} == []) // false
console.log({} == 0) // false
// Notes
// || with 2 truthy value
// then javascript will return the first truthy value
// exp: (5 || 10) // 5
// // 5 is truthy so it will return 5
// && with 2 truthy value
// then javascript will return the last truthy value
// console.log(5 && 10) // 10
// // 10 is last truthy value so it will return 10
// // 5 is truthy so it will return 10
// || with 2 falsy value
// then javascript will return the first falsy value
// console.log(0 || 0) // 0
// // 0 is falsy so it will return 0
// // 0 is falsy so it will return 0
// && with 2 falsy value
// then javascript will return the first falsy value
// console.log(0 && 0) // 0
// // 0 is falsy so it will return 0
// // 0 is falsy so it will return 0
// // 0 is falsy so it will return 0
// // 0 is falsy so it will return 0
// // 0 is falsy so it will return 0
// // 0 is falsy so it will return 0
// // 0 is falsy so it will return 0
// // 0 is falsy so it will return 0
typeof null // object
typeof undefined // undefined
typeof NaN // number
typeof 0 // number
typeof "" // string
typeof [] // object
typeof {} // object
typeof function () { } // function
//-------
setTimeout(() => {
console.log('check1')
}, 0)
Promise.resolve().then(() => {
console.log('check2')
})
// Promise.reject().then(() => {
// console.log('check3')
// })
process.nextTick(() => {
console.log('check5')
})
setImmediate(() => {
console.log('check6');
});
// setInterval(() => {
// // console.log('check4')
// })
//-------
// const [p, h = 20] = 4;
// console.log(p * h); // 1000
// javascript [] or {} consider truthy value
const arr6 = [1, 2];
for (var i = 0; i < arr6.length; i++) {
setTimeout(() => {
console.log(i)
}, [1000])
}
// UNIORY OPERATORE IN JAVASCRIPT CONVERT TO NUMBER