-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSQuestions.js
113 lines (85 loc) · 3.08 KB
/
JSQuestions.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
Note:- AP means Arun Prakash ( CEO of GUVI )
================================================================================
Question By AP:
console.lgo(mul(2)(3)(4))
//Expected Output: 24
Answer:
function mul(n1) {
return function(n2) {
return function(n3) {
return n1*n2*n3;
}
}
}
================================================================================
Question By AP:
var myconfusion = 1;
var output = (function() {
delete myconfusion;
return myconfusion;
}
) ();
console.log(output); // What is the output of this code
Answer:
1 /* cause delete checks only the functional block scope and returns true
even if it is not in the local scope */
Note:
Cross check with MDN which specifies this behaviour.
================================================================================
Question By AP:
var diffConfusion = { foo: 1}
var output = ( function() {
delete diffConfusion.foo;
return diffConfusion.foo;
}
)
console.log(output); // What is the output of this code
Answer:
undefined // Since the delete removes the foo key from dictionary.
================================================================================
Question By AP:
var z = 1, y = z = typeof y;
console.log(y); // What is the output of this code
Answer:
undefined /* since y is undefined at the time of resolving the typeof
returns a string called "undefined" which is stored in both z and y; */
================================================================================
Question By AP:
/* What is the difference between declaring function in the formats listed
below? */
var foo = function() {
// Some Code
}
function bar() {
// Some Code
}
/*
Answer:
Nothing really much but the first one is called anonymous function and the
second declaration is called Named function
Note:
They are useful because of higher order function. So, they need not to be
bothered about thinking about meaningful name for every argument functions.
*/
================================================================================
Question By AP:
bar();
(function abc() { console.log("Something") } ) ();
function bar() { console.log("bar got called") }
// What is the output of the code ?
Answer:
bar got called
Something
/* The reason is because of hoisting since IIFE is an expression it is
executed or interpreted at the time of evaluation and not in time of hoisting.
*/
================================================================================
Question By AP:
var counterArray = { A:3, B: 4};
counterArray["C"] = 1;
console.log( Object.keys(counterArray).length );
// What is the output of this code
Answer:
3 /* The Object method returns array of keys and the length of the array
is printed in console. */
================================================================================