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
+272-7Lines changed: 272 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -922,7 +922,28 @@ console.log(y); // 10
922
922
923
923
## Q 2.14. What is scope chain in javascript?
924
924
925
-
*ToDo*
925
+
The scope chain in JavaScript refers to the chain of nested scopes that a JavaScript program uses to look up variable and function references. When a variable or function is referenced in JavaScript code, the interpreter first looks for it in the current scope. If it's not found there, it moves up the scope chain to the next outer scope and looks for it there. It continues doing this until the variable or function is found or until it reaches the global scope.
926
+
927
+
**Example:**:
928
+
929
+
```js
930
+
let globalVar ="I'm a global variable";
931
+
932
+
functionouter() {
933
+
let outerVar ="I'm an outer variable";
934
+
935
+
functioninner() {
936
+
let innerVar ="I'm an inner variable";
937
+
console.log(innerVar); // "I'm an inner variable"
938
+
console.log(outerVar); // "I'm an outer variable"
939
+
console.log(globalVar); // "I'm a global variable"
940
+
}
941
+
942
+
inner();
943
+
}
944
+
945
+
outer();
946
+
```
926
947
927
948
<divalign="right">
928
949
<b><a href="#table-of-contents">↥ back to top</a></b>
@@ -2637,7 +2658,27 @@ SyntaxError: Rest element must be last element
2637
2658
2638
2659
## Q 7.20. What is difference between [] and new Array()?
2639
2660
2640
-
*ToDo*
2661
+
`[]` and `new Array()` are two different ways of creating an array, but they are functionally equivalent.
2662
+
2663
+
The primary difference between them is in how they are created and in their behavior when used with certain methods.
2664
+
2665
+
`[]` is a shorthand for creating a new array. It is the preferred way to create an array in most cases, because it's more concise and easier to read. For example:
2666
+
2667
+
```js
2668
+
constmyArray= []; // create a new empty array
2669
+
```
2670
+
2671
+
On the other hand, `new Array()` is a constructor function that creates a new array object. It can be used to create an array of a specific length or with specific elements. For example:
2672
+
2673
+
```js
2674
+
constmyArray=newArray(); // create a new empty array
2675
+
constmyOtherArray=newArray(3); // create a new array with a length of 3
2676
+
constmyThirdArray=newArray("a", "b", "c"); // create a new array with three elements
2677
+
```
2678
+
2679
+
One potential pitfall of using `new Array()` is that it can be ambiguous when you pass a single argument to the constructor. For example, `new Array(3)` creates an array with a length of 3, but `new Array("3")` creates an array with a single element, the string "3". This is because the argument is treated as the value of the first element when it's a non-negative integer, but as the length of the array when it's a string or a negative integer.
2680
+
2681
+
In summary, `[]` is the preferred way to create a new array in JavaScript, while `new Array()` is an alternative way that can be used when you need more control over the array's length or contents.
2641
2682
2642
2683
<divalign="right">
2643
2684
<b><a href="#table-of-contents">↥ back to top</a></b>
@@ -4120,7 +4161,27 @@ try {
4120
4161
4121
4162
## Q 9.30. How function overloading works in JavaScript?
4122
4163
4123
-
*ToDo*
4164
+
Function overloading refers to the ability to define multiple functions with the same name but with different parameters. In many programming languages, the function to be executed is determined at compile time based on the parameters provided. However, in JavaScript, function overloading does not work in the same way because JavaScript functions can be called with any number and type of arguments.
4165
+
4166
+
One way to achieve function overloading in JavaScript is by using conditional statements to determine the appropriate behavior based on the arguments passed to the function.
4167
+
4168
+
**Example**
4169
+
4170
+
```js
4171
+
functionmyFunction() {
4172
+
if (arguments.length===1) {
4173
+
console.log("Hello "+arguments[0]);
4174
+
} elseif (arguments.length===2) {
4175
+
console.log("Hello "+arguments[0] +" and "+arguments[1]);
4176
+
} else {
4177
+
console.log("Hello world");
4178
+
}
4179
+
}
4180
+
4181
+
myFunction(); // output: "Hello world"
4182
+
myFunction("Alice"); // output: "Hello Alice"
4183
+
myFunction("Bob", "Charlie"); // output: "Hello Bob and Charlie"
4184
+
```
4124
4185
4125
4186
<divalign="right">
4126
4187
<b><a href="#table-of-contents">↥ back to top</a></b>
@@ -5857,7 +5918,56 @@ An array is an object so it has all the same capabilities of an object plus a bu
5857
5918
5858
5919
## Q 11.33. What is difference between `{}` vs `new Object()`?
5859
5920
5860
-
*ToDo*
5921
+
**1. Object Literal Syntax (`{}`):**
5922
+
5923
+
Object literal syntax is a shorthand way of creating an object. We can create an object by placing a comma-separated list of key-value pairs inside curly braces `{ }`. The key represents a property name of the object and the value represents the value of that property.
The object constructor syntax is a way of creating an object using the `new` operator and the `Object` constructor function. We can create an empty object by calling the `Object` constructor without any arguments. We can also create an object by passing an object literal as an argument to the `Object` constructor.
5948
+
5949
+
**Syntax:**
5950
+
5951
+
```js
5952
+
let myObj =newObject();
5953
+
```
5954
+
5955
+
**Example:**
5956
+
5957
+
```js
5958
+
let person =newObject();
5959
+
person.name="John";
5960
+
person.age=30;
5961
+
person.city="New York";
5962
+
```
5963
+
5964
+
**Difference:**
5965
+
5966
+
The primary difference between using `{}` and `new Object()` to create an object is that the former uses object literal syntax while the latter uses object constructor syntax.
5967
+
5968
+
Object literals are more concise and easier to read and write, especially when creating objects with a small number of properties. On the other hand, object constructors are more flexible and can be used to create objects with properties that are not known at the time of object creation.
5969
+
5970
+
In general, it is recommended to use object literal syntax (`{}`) for creating objects unless there is a specific reason to use the object constructor syntax (`new Object()`).
5861
5971
5862
5972
<divalign="right">
5863
5973
<b><a href="#table-of-contents">↥ back to top</a></b>
@@ -7818,15 +7928,56 @@ The `XMLHTTPRequest()` object is an API which is used for fetching data from the
7818
7928
7819
7929
## Q. 15.14. How to get responses of multiple api calls, when some API fails?
7820
7930
7821
-
*ToDo*
7931
+
**Promise.allSettled():**
7932
+
7933
+
To handle failures in multiple API calls, `Promise.allSettled()` method can be used. The method returns a Promise that resolves after all of the given promises have either resolved or rejected. It returns an array of objects with two properties: status and value.
7934
+
7935
+
The status property is either "fulfilled" or "rejected", depending on whether the promise was resolved or rejected. The value property is the value of the promise if it was resolved or the reason for rejection if it was rejected.
console.log('Successful API calls:', successfulResults);
7956
+
console.log('Failed API calls:', failedResults);
7957
+
})
7958
+
.catch(error=>console.log('Error:', error));
7959
+
```
7822
7960
7823
7961
<div align="right">
7824
7962
<b><a href="#table-of-contents">↥ back to top</a></b>
7825
7963
</div>
7826
7964
7827
7965
## Q. 15.15. Explain the use of Promise.any()?
7828
7966
7829
-
*ToDo*
7967
+
The `Promise.any()` method is used to handle multiple promises simultaneously, and it resolves with the value of the first fulfilled promise.regardless of whether any of the other promises reject. If all of the promises reject, then `Promise.any()` rejects with an `AggregateError` object that contains an array of rejection reasons.
7968
+
7969
+
The `Promise.any()` method takes an iterable of Promises as an input, and returns a new Promise. Here's an example:
.catch((error) =>console.error(error)); // AggregateError: All promises were rejected
7979
+
```
7980
+
*Note: `Promise.any()` is not yet widely supported by all browsers, so you may need to use a polyfill or a different approach to achieve similar functionality in older browsers.*
7830
7981
7831
7982
<div align="right">
7832
7983
<b><a href="#table-of-contents">↥ back to top</a></b>
@@ -9034,11 +9185,125 @@ Microsoft formerly developed its own proprietary browser engines - Trident and E
9034
9185
</div>
9035
9186
9036
9187
#### Q 18.22. What is a Polyfill?
9188
+
9189
+
**Polyfill**
9190
+
9191
+
A polyfill is a piece of code (usually a JavaScript library) that provides modern functionality on older browsers that do not support it. It is a way to bring new features to old browsers by mimicking the behavior of modern JavaScript APIs.
9192
+
9193
+
For example, `Object.values()` was introduced in ES2017 and is not supported in some older browsers such as Internet Explorer and Safari 9. However, you can use a polyfill to add support for it in older browsers.
9194
+
9195
+
**Example**
9196
+
9197
+
```js
9198
+
// polyfill for the Object.values()
9199
+
if (!Object.values) {
9200
+
Object.values=function(obj) {
9201
+
var values = [];
9202
+
for (var key in obj) {
9203
+
if (obj.hasOwnProperty(key)) {
9204
+
values.push(obj[key]);
9205
+
}
9206
+
}
9207
+
return values;
9208
+
};
9209
+
}
9210
+
9211
+
// Now you can use Object.values() even in older browsers that don't support it natively
9212
+
constobj= { a:1, b:2, c:3 };
9213
+
constvalues=Object.values(obj);
9214
+
9215
+
console.log(values); // Output: [1, 2, 3]
9216
+
```
9217
+
9218
+
This code checks if the `Array.prototype.includes()` method is available in the current environment, and if not, it provides an implementation of the method that emulates the behavior of the modern API. This allows you to use the `Array.prototype.includes()` method in older browsers, even though it is not natively supported.
9219
+
9220
+
<div align="right">
9221
+
<b><a href="#table-of-contents">↥ back to top</a></b>
9222
+
</div>
9223
+
9224
+
9037
9225
#### Q 18.23. What is optional chaining in JavaScript?
9226
+
9227
+
**Optional chaining**
9228
+
9229
+
Optional chaining is a feature in JavaScript that allows you to safely access nested object properties or functions without worrying about whether the intermediate properties exist or not. It uses the `?.` operator to check for nullish (`null` or `undefined`) values and short-circuits the expression if it encounters one.
9230
+
9231
+
Optional chaining was introduced in ECMAScript 2020 (ES11) and is supported in most modern browsers, but not in older ones. To use it in older browsers, you can use a transpiler like Babel, or write your code with alternative techniques like conditional statements or the `&&` operator.
<b><a href="#table-of-contents">↥ back to top</a></b>
9257
+
</div>
9258
+
9038
9259
#### Q 18.24. How could you make sure a const value is garbage collected?
9260
+
9261
+
In JavaScript, garbage collection is automatically performed by the browser or the JavaScript engine. When a value is no longer being used or referenced, it becomes eligible for garbage collection.
9262
+
9263
+
In the case of a const value, the same rules apply as with any other value. As long as the const value is not referenced by any other variables or objects in the program, it will become eligible for garbage collection when the variable goes out of scope.
9264
+
9265
+
9266
+
Here are some best practices to follow when working with const variables:
9267
+
9268
+
1. Only use `const` whenyouknowthatthevalueshouldnotbereassigned.
9269
+
9270
+
2. If you ever want to change the contents of the variable for any reason in the future, then don't declare it as const.
9271
+
9272
+
3. Use `let` or `var` instead of `const` if you need to reassign the value.
9273
+
9274
+
4. To ensure that a const value is garbage collected, remove all references to it.
9275
+
9276
+
**Example**
9277
+
9278
+
```
9279
+
const myObj = {name: 'John', age: 30};
9280
+
9281
+
// Use myObj...
9282
+
9283
+
// Remove all references to myObj
9284
+
myObj = null;
9285
+
```
9286
+
9287
+
In this example, setting `myObj` to `null` removes the only reference to the object, allowing it to be garbage collected.
9288
+
9289
+
<div align="right">
9290
+
<b><a href="#table-of-contents">↥ back to top</a></b>
9291
+
</div>
9292
+
9039
9293
#### Q 18.25. How Garbage Collection works in JavaScript?
9040
9294
9041
-
*ToDo*
9295
+
JavaScript has an automatic garbage collector that periodically frees up memory that is no longer being used by the program. The garbage collector works by identifying "garbage" values that are no longer accessible or needed by the program and freeing up the memory they occupy.
9296
+
9297
+
**Example**
9298
+
9299
+
```js
9300
+
let a = { b: { c: { d: "Hello" } } };
9301
+
let e = a.b.c;
9302
+
a = null;
9303
+
```
9304
+
In this code, the object `{ b: { c: { d: "Hello" } } }` is created and assigned to the variable a. The variable e is then assigned a reference to the nested object `{ c: { d: "Hello" } }`. Finally, the variable a is set to null, which means that the original object `{ b: { c: { d: "Hello" } } }` is no longer accessible by the program.
9305
+
9306
+
At this point, the garbage collector will identify the object `{ b: { c: { d: "Hello" } } }` as "garbage" because it is no longer accessible by the program. The garbage collector will then free up the memory occupied by this object.
9042
9307
9043
9308
<div align="right">
9044
9309
<b><a href="#table-of-contents">↥ back to top</a></b>
0 commit comments