Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c09647

Browse files
committed
new JS questions
1 parent 0e0090d commit 9c09647

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

β€ŽREADME.md

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Prepare for your next 2023 JavaScript interview with these tricky
44
githubPath: "https://github.com/Vasu7389/JavaScript-Interview-Questions-2023"
55
---
66

7-
<span style=" font-size: 0.8rem; border-bottom: 1px solid grey;"> Updated Dec 31, 2022 </span>
7+
<span style=" font-size: 0.8rem; border-bottom: 1px solid grey;"> Updated Jan 04, 2023 </span>
88

99
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).
1010

@@ -88,3 +88,115 @@ This is because when an object is assigned to a variable, the variable stores a
8888
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.
8989

9090
</details>
91+
92+
<details>
93+
<summary>
94+
<h3>3. Guess the output of this JavaScript code?
95+
96+
```js
97+
const obj = {
98+
a: "foo",
99+
b: function () {
100+
console.log(this.a);
101+
},
102+
};
103+
104+
const c = 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+
const x = { foo: 1 };
127+
const y = { foo: 2 };
128+
129+
function addFoo(obj) {
130+
return obj.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+
const arr = [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+
const arr = [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 (function f() {}) {
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

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /