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
+93-1Lines changed: 93 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,12 @@ Top JavaScript interview questions
21
21
| 13 |[What is Temporal Dead Zone](#13-what-is-temporal-dead-zone)|
22
22
| 14 |[What is IIFE](#14-what-is-iife)|
23
23
| 15 |[What is Hoisting](#15-what-is-hoisting)|
24
-
24
+
| 16 |[What are Cookies](#16-what-are-cookies)|
25
+
| 17 |[What is memoization](#17-what-is-memoization)|
26
+
| 18 |[Difference between var, let and const](#18-difference-between-var-let-and-const)|
27
+
| 19 |[What is a callback function](#19-what-is-a-callback-function)|
28
+
| 20 |[Is it possible to have both local and global variables with the same name](#20-is-it-possible-to-have-both-local-and-global-variables-with-the-same-name)|
29
+
| 21 |[Difference between Local Storage and Session Storage](#21-difference-between-local-storage-and-session-storage)|
25
30
26
31
### 1. What is JavaScript
27
32
* JavaScript is a scripting language used to create dynamic and interactive websites. It is supported by all major web browsers.
@@ -205,6 +210,93 @@ The output of above code will be "demo console" because the function declaration
205
210
+ Only the declaration is hoisted, not the initialization.
206
211
```
207
212
213
+
### 16. What are Cookies
214
+
In javascript, a cookie is a piece of data, stored in small text files, on the user's computer by the browser.
215
+
Cookies are set, read and deleted using the document.cookie property
216
+
217
+
```javascript
218
+
//**Set a cookie**
219
+
document.cookie="username=surbhi";
220
+
221
+
//**Read a cookie**
222
+
let cookie_variable =document.cookie;
223
+
224
+
//**Delete a cookie** (set the expiration date to a past date/time)
Memoization is a technique used in JavaScript to optimize the performance of functions by caching the results of expensive function calls, based on their input parameters.
230
+
For e.g., If a function is called multiple times with the same input parameters, then it will perform the same calculations each time. By memoizing the function, we can use the cached result.
231
+
232
+
```js
233
+
functionmemoizedAdd(num1, num2) {
234
+
let cache = {};
235
+
returnfunction (a, b) {
236
+
constkey= num1 +' and '+ num2;
237
+
if (key in cache) {
238
+
console.log('Retrieving from cache:', key);
239
+
return cache[key];
240
+
} else {
241
+
console.log('Calculating result:', key);
242
+
constresult= num1 + num2;
243
+
cache[key] = result;
244
+
return result;
245
+
}
246
+
};
247
+
}
248
+
249
+
constadd=memoizedAdd();
250
+
console.log(add(2, 3)); // "Calculating result:", "2 and 3", output=====> 5
251
+
console.log(add(2, 3)); // "Retrieving from cache:", "2 and 3" output=====> 5
252
+
```
253
+
254
+
### 18. Difference between var, let and const
255
+
In javascript, there are 3 ways to declare a variable - var, let, and const. However, they have some differences in their behavior and scope.
256
+
| var | let | const |
257
+
| --- | --- | --- |
258
+
| Function scoped | Block scoped | Block scoped |
259
+
| Can be re-declared in the same scope | Can not be re-declared in the same scope | Can not be re-declared in the same scope |
260
+
| Can be updated in the same scope | Can be updated in the same scope | Can not be updated in the same scope |
261
+
| Hoisted to the top of their scope | Hoisted to the top but are not initialized | Hoisted to the top but are not initialized |
262
+
| Can be declared without being initialized |Can be declared without being initialized | Can not be declared without being initialized |
263
+
264
+
### 19. What is a callback function
265
+
A callback function is a function that is passed as an argument to another function. The function that receives the callback as an argument can then invoke the callback at any time during its execution.
266
+
```js
267
+
functionmessage(callback) {
268
+
console.log('Hi, i am message function');
269
+
callback();
270
+
}
271
+
// callback function
272
+
functioncallBackFun() {
273
+
console.log('Hey, I am a callback function');
274
+
}
275
+
// passing callback function as an argument to message function
276
+
message(callBackFun);
277
+
```
278
+
279
+
### 20. Is it possible to have both local and global variables with the same name
280
+
Yes, we can have both with the same name. But when we do this, the local variable will take precedence over the global variable within the scope of the function or block of code in which it is declared.
281
+
282
+
However, outside of that scope, the global variable will still be accessible using its name.
283
+
284
+
```js
285
+
let a =10;
286
+
functionDemo(){
287
+
let a =20;
288
+
console.log(a, "local scope");
289
+
}
290
+
Demo();
291
+
console.log(a, "global scope");
292
+
```
293
+
294
+
### 21. Difference between Local Storage and Session Storage
295
+
Local storage and session storage are web storage provided by web browsers to store data on the client-side
296
+
297
+
**Local Storage** - Data stored in local storage will persist even after the browser window is closed or the user navigates away from the website and it is available across all windows and tabs of the same origin.
298
+
299
+
**Session Storage** - Data stored in session storage will be deleted when the browser window is closed or the session ends and it is available only within the same window or tab that created the data.
0 commit comments