You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<spanstyle="font-size: 0.8rem; border-bottom: 1pxsolidgrey;"> Updated Dec 31, 2022 </span>
7
+
<spanstyle="font-size: 0.8rem; border-bottom: 1pxsolidgrey;"> Updated Jan 04, 2023 </span>
8
8
9
9
In this article, we will cover a range of JavaScript interview questions, including those related to the latest versions of the language (ES6, ES7, ES8, and ES9).
10
10
@@ -88,3 +88,115 @@ This is because when an object is assigned to a variable, the variable stores a
88
88
In this case, the value of the key property for obj1 was changed to "new value" using the obj1 variable, which affected the value of the key property when accessed using the obj3 variable, because both variables reference the same object. However, the value of the key property for obj2 was not affected, because the obj2 variable was reassigned to reference a new object.
89
89
90
90
</details>
91
+
92
+
<details>
93
+
<summary>
94
+
<h3>3. Guess the output of this JavaScript code?
95
+
96
+
```js
97
+
constobj= {
98
+
a:"foo",
99
+
b:function () {
100
+
console.log(this.a);
101
+
},
102
+
};
103
+
104
+
constc=obj.b;
105
+
106
+
obj.b();
107
+
c();
108
+
```
109
+
110
+
</h3>
111
+
</summary>
112
+
113
+
Answer - foo, undefined
114
+
115
+
When the method obj.b is called directly on obj, the output will be "foo". This is because this refers to the object that the method is called on, and obj.a is equal to "foo".
116
+
117
+
When the variable c is assigned the value of obj.b, it is a reference to the function itself and not the object obj. When c is called, it is not called on an object, so this will not refer to obj and the value of this.a is undefined. As a result, the output when calling c() will be undefined.
118
+
119
+
</details>
120
+
121
+
<details>
122
+
<summary>
123
+
<h3>4. Guess the output of this code?
124
+
125
+
```js
126
+
constx= { foo:1 };
127
+
consty= { foo:2 };
128
+
129
+
functionaddFoo(obj) {
130
+
returnobj.foo+1;
131
+
}
132
+
133
+
console.log(addFoo(x));
134
+
console.log(addFoo(y));
135
+
```
136
+
137
+
</h3>
138
+
</summary>
139
+
Answer - 2, 3
140
+
141
+
The addFoo function takes an object as an argument and returns the value of obj.foo + 1. When addFoo is called with x as the argument, the output will be 2, because x.foo is equal to 1. When addFoo is called with y as the argument, the output will be 3, because y.foo is equal to 2.
142
+
143
+
</details>
144
+
<details>
145
+
<summary>
146
+
<h3>5. Guess the output of below JavaScript code?
147
+
148
+
```js
149
+
constarr= [1, 2, 3, 4, 5];
150
+
151
+
for (var i =0; i <arr.length; i++) {
152
+
setTimeout(function () {
153
+
console.log(i);
154
+
}, 1000);
155
+
}
156
+
```
157
+
158
+
</h3>
159
+
</summary>
160
+
Answer - 5, 5, 5, 5, 5
161
+
162
+
The setTimeout function is called inside of a loop that iterates through the elements in the arr array. The setTimeout function will execute its callback function after a delay of 1000 milliseconds. However, by the time the delay has elapsed and the callback function is called, the loop will have already completed and the value of i will be 5. As a result, the output will be 5 printed five times.
163
+
164
+
</details>
165
+
<details>
166
+
<summary>
167
+
<h3>6. Guess the output of this JavaScript code?
168
+
169
+
```js
170
+
constarr= [1, 2, 3, 4, 5];
171
+
172
+
arr.forEach(function (element) {
173
+
console.log(element);
174
+
});
175
+
```
176
+
177
+
</h3>
178
+
</summary>
179
+
Answer - 1, 2, 3, 4, 5
180
+
181
+
The forEach method is called on the arr array and a callback function is passed as an argument. The callback function will be executed for each element in the array, with the element passed as an argument to the callback. As a result, the output will be the elements of the array, 1, 2, 3, 4, and 5, printed on separate lines.
182
+
183
+
</details>
184
+
<details>
185
+
<summary>
186
+
<h3>7. Guess the output of this code?
187
+
188
+
```js
189
+
let x =1;
190
+
191
+
if (functionf() {}) {
192
+
x +=typeof f;
193
+
}
194
+
195
+
console.log(x);
196
+
```
197
+
198
+
</h3>
199
+
</summary>
200
+
Answer - 1undefined
201
+
202
+
The if statement is evaluating the function f as a boolean value. In JavaScript, functions are truthy values, so the condition will evaluate to true and the code block inside the if statement will be executed. The value of x is then incremented by the string "function", which is the result of calling typeof f.
0 commit comments