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
By default, event handlers triggered in event bubbling phase as shown below,
2022
2034
2023
2035
```javascript
2024
-
<div>
2036
+
<div>
2025
2037
<button class="child">Hello</button>
2026
2038
</div>
2027
2039
@@ -2108,7 +2120,9 @@
2108
2120
91. ### What is the difference between native, host and user objects
2109
2121
2110
2122
`Native objects` are objects that are part of the JavaScript language defined by the ECMAScript specification. For example, String, Math, RegExp, Object, Function etc core objects defined in the ECMAScript spec.
2111
-
`Host objects` are objects provided by the browser or runtime environment (Node). For example, window, XmlHttpRequest, DOM nodes etc are considered as host objects.
2123
+
`Host objects` are objects provided by the browser or runtime environment (Node).
2124
+
2125
+
For example, window, XmlHttpRequest, DOM nodes etc are considered as host objects.
2112
2126
`User objects` are objects defined in the javascript code. For example, User objects created for profile information.
2113
2127
2114
2128
**[⬆ Back to Top](#table-of-contents)**
@@ -2615,13 +2629,13 @@
2615
2629
Object.entries(obj).length === 0 && obj.constructor === Object; // Since date object length is 0, you need to check constructor check as well
2616
2630
```
2617
2631
2618
-
1. **Using Object keys(ECMA 5+):** You can use object keys length along with constructor type.
2632
+
2. **Using Object keys(ECMA 5+):** You can use object keys length along with constructor type.
2619
2633
2620
2634
```javascript
2621
2635
Object.keys(obj).length === 0 && obj.constructor === Object; // Since date object length is 0, you need to check constructor check as well
2622
2636
```
2623
2637
2624
-
1. **Using for-in with hasOwnProperty(Pre-ECMA 5):** You can use a for-in loop along with hasOwnProperty.
2638
+
3. **Using for-in with hasOwnProperty(Pre-ECMA 5):** You can use a for-in loop along with hasOwnProperty.
2625
2639
2626
2640
```javascript
2627
2641
function isEmpty(obj) {
@@ -2771,7 +2785,7 @@
2771
2785
object.key3 = "value3";
2772
2786
```
2773
2787
2774
-
1.**Using square bracket notation:** This solution is useful when the name of the property is dynamically determined.
2788
+
2.**Using square bracket notation:** This solution is useful when the name of the property is dynamically determined.
174. ### Can you apply chaining on conditional operator
3343
3357
3344
-
Yes, you can apply chaining on conditional operators similar to if … else if … else if … else chain. The syntax is going to be as below,
3358
+
Yes, you can apply chaining on conditional operators similar to **if … else if … else if … else** chain. The syntax is going to be as below,
3345
3359
3346
3360
```javascript
3347
3361
functiontraceValue(someParam) {
@@ -3381,13 +3395,13 @@
3381
3395
window.onload=function ...
3382
3396
```
3383
3397
3384
-
1. **document.onload:**
3398
+
2. **document.onload:**
3385
3399
3386
3400
```javascript
3387
3401
document.onload=function ...
3388
3402
```
3389
3403
3390
-
1. **body onload:**
3404
+
3. **body onload:**
3391
3405
3392
3406
```javascript
3393
3407
<body onload="script();">
@@ -4299,13 +4313,13 @@
4299
4313
objectName.property;
4300
4314
```
4301
4315
4302
-
1. **Square brackets notation:** It uses square brackets for property access
4316
+
2. **Square brackets notation:** It uses square brackets for property access
4303
4317
4304
4318
```javascript
4305
4319
objectName["property"];
4306
4320
```
4307
4321
4308
-
1. **Expression notation:** It uses expression in the square brackets
4322
+
3. **Expression notation:** It uses expression in the square brackets
4309
4323
4310
4324
```javascript
4311
4325
objectName[expression];
@@ -4427,7 +4441,9 @@
4427
4441
Synchronous iteration was introduced in ES6 and it works with below set of components,
4428
4442
4429
4443
**Iterable:** It is an object which can be iterated over via a method whose key is Symbol.iterator.
4444
+
4430
4445
**Iterator:** It is an object returned by invoking `[Symbol.iterator]()` on an iterable. This iterator object wraps each iterated element in an object and returns it via `next()` method one by one.
4446
+
4431
4447
**IteratorResult:** It is an object returned by `next()` method. The object contains two properties; the `value` property contains an iterated element and the `done` property determines whether the element is the last element or not.
4432
4448
4433
4449
Let's demonstrate synchronous iteration with an array as below,
@@ -6058,7 +6074,9 @@
6058
6074
6059
6075
327. ### How do you avoid receiving postMessages from attackers
6060
6076
6061
-
Since the listener listens for any message, an attacker can trick the application by sending a message from the attacker’s origin, which gives an impression that the receiver received the message from the actual sender’s window. You can avoid this issue by validating the origin of the message on the receiver's end using the “message.origin” attribute. For examples, let's check the sender's origin [http://www.some-sender.com](http://www.some-sender.com) on receiver side [www.some-receiver.com](www.some-receiver.com),
6077
+
Since the listener listens for any message, an attacker can trick the application by sending a message from the attacker’s origin, which gives an impression that the receiver received the message from the actual sender’s window. You can avoid this issue by validating the origin of the message on the receiver's end using the “message.origin” attribute.
6078
+
6079
+
For example, let's check the sender's origin [http://www.some-sender.com](http://www.some-sender.com) on receiver side [www.some-receiver.com](www.some-receiver.com),
6062
6080
6063
6081
```javascript
6064
6082
//Listener on http://www.some-receiver.com/
@@ -7084,6 +7102,7 @@
7084
7102
The main sources of microtasks are Promise.resolve, Promise.reject, MutationObservers, IntersectionObservers etc
7085
7103
7086
7104
**Note:** All of these microtasks are processed in the same turn of the event loop.
7105
+
7087
7106
**[⬆ Back to Top](#table-of-contents)**
7088
7107
7089
7108
389. ### What are different event loops
@@ -7094,9 +7113,9 @@
7094
7113
2. The Node.js Event Loop
7095
7114
7096
7115
7097
-
- Browser Event Loop: The Browser Event Loop is used in client-side JavaScript applications and is responsible for handling events that occur within the browser environment, such as user interactions (clicks, keypresses, etc.), HTTP requests, and other asynchronous actions.
7116
+
- Browser Event Loop: The Browser Event Loop is used in client-side JavaScript applications and is responsible for handling events that occur within the browser environment, such as user interactions (clicks, keypresses, etc.), HTTP requests, and other asynchronous actions.
7098
7117
7099
-
- The Node.js Event Loop is used in server-side JavaScript applications and is responsible for handling events that occur within the Node.js runtime environment, such as file I/O, network I/O, and other asynchronous actions.
7118
+
- The Node.js Event Loop is used in server-side JavaScript applications and is responsible for handling events that occur within the Node.js runtime environment, such as file I/O, network I/O, and other asynchronous actions.
7100
7119
7101
7120
**[⬆ Back to Top](#table-of-contents)**
7102
7121
@@ -8102,25 +8121,25 @@
8102
8121
8103
8122
The _debounce()_ function can be used on input, button and window events.
449. ### What is the purpose of the this keyword in JavaScript?
8424
8443
* The `this` keyword in JavaScript is a special variable that is used within a function to refer to the object on which the function is invoked. The value of this depends on how the function is called. It allows functions to access and interact with the object they are bound to.
8425
8444
* The this keyword in JavaScript is a reference to the object that owns or invokes the current function. Its value is determined by the calling context.
8426
-
**Example 1: this in a Global Context**
8427
-
```javascript
8428
-
console.log(this);
8429
-
```
8430
-
* In a global context, this refers to the global object (e.g., window in a browser).
8431
8445
8432
-
**Example 2: this in a Function**
8433
-
```javascript
8434
-
functiondisplayThis() {
8446
+
**Example 1: this in a Global Context**
8447
+
```javascript
8435
8448
console.log(this);
8436
-
}
8437
-
8438
-
displayThis();
8439
-
```
8440
-
* In a regular function, this refers to the global object.
8449
+
```
8450
+
* In a global context, this refers to the global object (e.g., window in a browser).
8441
8451
8442
-
**Example 3: this in a Method**
8443
-
```javascript
8444
-
constperson= {
8445
-
name:'John',
8446
-
greet:function() {
8447
-
console.log('Hello, '+this.name);
8452
+
**Example 2: this in a Function**
8453
+
```javascript
8454
+
functiondisplayThis() {
8455
+
console.log(this);
8448
8456
}
8449
-
};
8450
8457
8451
-
person.greet();
8452
-
```
8453
-
* In a method, this refers to the object that owns the method (person in the case).
8458
+
displayThis();
8459
+
```
8460
+
* In a regular function, this refers to the global object.
* In an event handler, this refers to the element that triggered the event (the button in this case).
8462
8482
8463
8483
**[⬆ Back to Top](#table-of-contents)**
8464
8484
@@ -8475,6 +8495,7 @@ Here are some common use cases of closures:
8475
8495
* Memoization: Closures can be used for memoization, a technique to optimize performance by caching the results of expensive function calls. The inner function can remember the results of previous calls and return the cached result if the same input is provided again.
8476
8496
8477
8497
* iterators and Generators: Closures can be used to create iterators and generators, which are essential for working with collections of data in modern JavaScript.
8498
+
8478
8499
**[⬆ Back to Top](#table-of-contents)**
8479
8500
8480
8501
451. ### What are the phases of execution context?
@@ -8589,7 +8610,7 @@ The execution context is created when a function is called. The function's code
8589
8610
constnums= [1, 2, 3, 4, 5, 6];
8590
8611
constevenNums=nums.myFilter(x=> x %2);
8591
8612
console.log(evenNums); // [2, 4, 6]
8592
-
```
8613
+
```
8593
8614
3. **reduce:**
8594
8615
8595
8616
The built-in `Array.reduce` method syntax will be helpful to write our own polyfill. The reduce method takes the callback function as first argument and the initial value as second argument.
@@ -8768,12 +8789,12 @@ The execution context is created when a function is called. The function's code
8768
8789
8769
8790
**Note:** You should only use either async or defer attribute if the `src` attribute is present.
8770
8791
8771
-
**[⬆ Back to Top](#table-of-contents)**
8792
+
**[⬆ Back to Top](#table-of-contents)**
8772
8793
8773
8794
464. ### What is Lexical Scope?
8774
8795
8775
8796
Lexical scope is the ability for a function scope to access variables from the parent scope.
8776
-
8797
+
```js
8777
8798
<script>
8778
8799
functionx(){
8779
8800
var a=10;
@@ -8785,6 +8806,7 @@ The execution context is created when a function is called. The function's code
8785
8806
}
8786
8807
x();
8787
8808
</script>
8809
+
```
8788
8810
8789
8811
**[⬆ Back to Top](#table-of-contents)**
8790
8812
@@ -11795,6 +11817,35 @@ If a function is called with `undefined`, the `undefined` value is treated as a
11795
11817
</details>
11796
11818
11797
11819
**[⬆ Back to Top](#table-of-contents)**
11820
+
11821
+
11822
+
#### 86. What is the output of below code?
11823
+
11824
+
```javascript
11825
+
functionfunc(a, b=2) {
11826
+
console.log(arguments.length);
11827
+
}
11828
+
11829
+
func(undefined);
11830
+
func();
11831
+
```
11832
+
11833
+
- 1: 1, 0
11834
+
- 2: 0, 0
11835
+
- 3: 0, 1
11836
+
- 4: 1, 1
11837
+
11838
+
<details><summary><b>Answer</b></summary>
11839
+
<p>
11840
+
11841
+
##### Answer: 1
11842
+
11843
+
If a function is called with `undefined`, the `undefined` value is treated as a parameter. But if the function is not passed with any parameters, the `arguments` object doesn't include any argument eventhough the function has default function parameter. Hence, the function invocation with `undefined` has one argument and function call without any arguments has 0 arguments.
0 commit comments