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 3d78fd5

Browse files
Added Question-Answer 16-21
1 parent 83ba570 commit 3d78fd5

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

‎README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ Top JavaScript interview questions
2121
| 13 | [What is Temporal Dead Zone](#13-what-is-temporal-dead-zone) |
2222
| 14 | [What is IIFE](#14-what-is-iife) |
2323
| 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) |
2530

2631
### 1. What is JavaScript
2732
* 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
205210
+ Only the declaration is hoisted, not the initialization.
206211
```
207212

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)
225+
document.cookie = "username=; expires=1970年1月01日 00:00:00 UTC; path=/;";
226+
```
227+
228+
### 17. What is memoization
229+
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+
function memoizedAdd(num1, num2) {
234+
let cache = {};
235+
return function (a, b) {
236+
const key = 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+
const result = num1 + num2;
243+
cache[key] = result;
244+
return result;
245+
}
246+
};
247+
}
248+
249+
const add = 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+
function message(callback) {
268+
console.log('Hi, i am message function');
269+
callback();
270+
}
271+
// callback function
272+
function callBackFun() {
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+
function Demo(){
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.
208300

209301
******************************In progress
210302

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /