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
Copy file name to clipboardExpand all lines: Readme.md
+240Lines changed: 240 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,3 +111,243 @@ const sortedAges = students
111
111
[Submit your solution](https://github.com/MenaiAla/javascript-interview-coding-questions-2023/pulls)
112
112
113
113
</details>
114
+
115
+
4. What is the output? and why?
116
+
117
+
```javascript
118
+
let variable;
119
+
console.log(typeof variable);
120
+
```
121
+
122
+
<details>
123
+
<summary>
124
+
Solution
125
+
</summary>
126
+
<br>
127
+
128
+
Output: `undefined`.
129
+
130
+
Because we **defined** the variable without **assigning** it any value, and the default value is `undefined`.
131
+
132
+
</details>
133
+
134
+
5. What is the output? and why?
135
+
136
+
```javascript
137
+
let variable =null;
138
+
console.log(typeof variable);
139
+
```
140
+
141
+
<details>
142
+
<summary>
143
+
Solution
144
+
</summary>
145
+
<br>
146
+
147
+
Output: `object`.
148
+
149
+
JavaScript has **7 primitive types**. All primitive types , except `null` can be tested with `typeof` operator.
150
+
`typeof null` returns `object`. If we want to check for `null` we have to use `===null`.
151
+
152
+
| Type |`typeof` return | Wrapper |
153
+
| --------- | --------------- | ------- |
154
+
| Null |`object`| N/A |
155
+
| Undefined |`undefined`| N/A |
156
+
| Boolean |`boolean`| Boolean |
157
+
| Number |`number`| Number |
158
+
| BigInt |`bigint`| BigInt |
159
+
| String |`string`| String |
160
+
| Symbol |`symbol`| Symbol |
161
+
162
+
</details>
163
+
164
+
6. What is the output? and why?
165
+
166
+
```javascript
167
+
console.log(variable);
168
+
let variable =1;
169
+
```
170
+
171
+
<details>
172
+
<summary>
173
+
Solution
174
+
</summary>
175
+
<br>
176
+
177
+
Output: `ReferenceError: Cannot access 'variable' before initialization`.
178
+
179
+
Javascript has a default behavior which is moving the declaration to the top of the current scope (script or function). We call this behavior **Hoisting**.
180
+
181
+
In JavaScript we can declare a variable after it has been used. Hoisting is ofter considered a feature of `var` declarations.
182
+
183
+
However, we can consider the following behaviors as Hoisting:
| Value | Being able to use a variable's value in its scope before the line it is declared |
188
+
| Declaration | Being able to reference a variable in its scope before the line it is declared, without throwing a `ReferenceError`, but the value is always `undefined`. |
189
+
190
+
`let` allows you to declare variables that are limited to the scope of a `block` statement, or expression on which it is used, unlike the `var` keyword, which declares a variable globally, or locally to an entire function regardless of block scope. The other difference between `var` and `let` is that the latter can only be accessed after its declaration is reached.
191
+
192
+
For this reason, `let` declarations are commonly regarded as non-hoisted.
193
+
194
+
</details>
195
+
196
+
7. What is the output? and why?
197
+
198
+
```javascript
199
+
console.log(variable);
200
+
variable =1;
201
+
```
202
+
203
+
<details>
204
+
<summary>
205
+
Solution
206
+
</summary>
207
+
<br>
208
+
209
+
Output: `ReferenceError: variable is not defined`.
210
+
211
+
Because we access a variable that **does not exist** in the scope. This exception occurs when there is a non-existent variable referenced somewhere.
212
+
213
+
In JavaScript we declare variables using the keywords:
214
+
215
+
1.`var`
216
+
2.`let`
217
+
3.`const`
218
+
219
+
</details>
220
+
221
+
8. Create a `counter` function which has `increment` and `getValue` functions.
222
+
223
+
<details>
224
+
<summary>
225
+
Solution
226
+
</summary>
227
+
228
+
```javascript
229
+
functioncounter() {
230
+
let counter =0;
231
+
return {
232
+
increment:function (value) {
233
+
return counter + value;
234
+
},
235
+
getValue:function () {
236
+
return counter;
237
+
},
238
+
};
239
+
}
240
+
```
241
+
242
+
</details>
243
+
244
+
9. Write a function `concatenate` which concatenates `firstName` and `lastName` (`concatenate(firstName)(lastName)`).
> **Function Currying** is a concept of breaking a function with many arguments into many functions with single argument in such a way, that the output is same. In other words, its a technique of simplifying a multi-valued argument function into single-valued argument multi-functions.
260
+
261
+
<details>
262
+
<summary>
263
+
Solution
264
+
</summary>
265
+
<br>
266
+
The concept of currying function is named after the <ahref="https://en.wikipedia.org/wiki/Haskell_Curry">Haskell Brooks Curry</a> who developed it.
267
+
268
+
> By definition, Currying is a process of transforming a function with higher arity to a function with lower arity. Curry functions are higher order functions which takes a function as input and returns a function that accepts arguments of the input function and either invokes that function returning its result (if at least arity number of arguments have been provided) or returns a function that accepts the remaining arguments and so on
269
+
270
+
`curry :: ((a,b)=>c) -> a -> b -> c`
271
+
272
+
```javascript
273
+
constcurry= (fn) => {
274
+
constarity=fn.length;
275
+
returnfunctionf1(...args) {
276
+
if (args.length>= arity) {
277
+
returnfn(...args);
278
+
} else {
279
+
returnfunctionf2(...moreArgs) {
280
+
returnf1(...args.concat(moreArgs));
281
+
};
282
+
}
283
+
};
284
+
};
285
+
```
286
+
287
+
</details>
288
+
289
+
11. Write a function that gets an array and an element and returns an array with this element.
290
+
291
+
<details>
292
+
<summary>
293
+
Solution
294
+
</summary>
295
+
296
+
```javascript
297
+
// Using push method ( not recommended )
298
+
constpushElement= (arr, el) => {
299
+
arr.push(el);
300
+
return arr;
301
+
};
302
+
// Using spread operator
303
+
constpushElement= (arr, el) => [...arr, el];
304
+
console.log(pushElement([1, 2, 3], 4));
305
+
```
306
+
307
+
</details>
308
+
309
+
12. Write a function that merges two arrays and returns an array.
0 commit comments