Skip to content

Commit 24f0468

Browse files
committed
Fix code alignment issues
1 parent f16bfb8 commit 24f0468

File tree

3 files changed

+111
-61
lines changed

3 files changed

+111
-61
lines changed

README.md

+111-60
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
1717
---
1818

19+
<div>
20+
<p align="center">
21+
<a href="https://www.frontendlead.com/coding-questions?utm_source=github&utm_medium=referral&ut%0Dm_campaign=sudheerj-js">
22+
<img src="./images/collab/frontendlead-banner.png" alt="FrontEndLead JavaScript Interview Questions" width="100%">
23+
</a>
24+
</p>
25+
</div>
26+
27+
> 🚀 Ace Javascript interview questions with solutions from FAANG+ companies! [Try FrontendLead →](https://www.frontendlead.com/coding-questions?utm_source=github&utm_medium=referral&ut%0Dm_campaign=sudheerj-js) 🚀
28+
29+
---
30+
1931
<p align="center">
2032
<a href=https://zerotomastery.io/?utm_source=github&utm_medium=sponsor&utm_campaign=javascript-interview-questions>
2133
<img src=https://process.fs.teachablecdn.com/ADNupMnWyR7kCWRvm76Laz/resize=height:70/https://www.filepicker.io/api/file/AKYtjj5SSGyJuyZrkAB2 alt="ZTM Logo" width="100" height="50">
@@ -2021,7 +2033,7 @@
20212033
By default, event handlers triggered in event bubbling phase as shown below,
20222034
20232035
```javascript
2024-
<div>
2036+
<div>
20252037
<button class="child">Hello</button>
20262038
</div>
20272039
@@ -2108,7 +2120,9 @@
21082120
91. ### What is the difference between native, host and user objects
21092121
21102122
`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.
21122126
`User objects` are objects defined in the javascript code. For example, User objects created for profile information.
21132127
21142128
**[⬆ Back to Top](#table-of-contents)**
@@ -2615,13 +2629,13 @@
26152629
Object.entries(obj).length === 0 && obj.constructor === Object; // Since date object length is 0, you need to check constructor check as well
26162630
```
26172631

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.
26192633

26202634
```javascript
26212635
Object.keys(obj).length === 0 && obj.constructor === Object; // Since date object length is 0, you need to check constructor check as well
26222636
```
26232637

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.
26252639

26262640
```javascript
26272641
function isEmpty(obj) {
@@ -2771,7 +2785,7 @@
27712785
object.key3 = "value3";
27722786
```
27732787

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.
27752789

27762790
```javascript
27772791
obj["key3"] = "value3";
@@ -3157,7 +3171,7 @@
31573171
document.getElementById("title").style.fontSize = "30px";
31583172
```
31593173

3160-
1. **Using ClassName property:** It is easy to modify element class using className property
3174+
2. **Using ClassName property:** It is easy to modify element class using className property
31613175

31623176
```javascript
31633177
document.getElementById("title").className = "custom-title";
@@ -3341,7 +3355,7 @@
33413355
33423356
174. ### Can you apply chaining on conditional operator
33433357
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,
33453359
33463360
```javascript
33473361
function traceValue(someParam) {
@@ -3381,13 +3395,13 @@
33813395
window.onload = function ...
33823396
```
33833397

3384-
1. **document.onload:**
3398+
2. **document.onload:**
33853399

33863400
```javascript
33873401
document.onload = function ...
33883402
```
33893403

3390-
1. **body onload:**
3404+
3. **body onload:**
33913405

33923406
```javascript
33933407
<body onload="script();">
@@ -4299,13 +4313,13 @@
42994313
objectName.property;
43004314
```
43014315

4302-
1. **Square brackets notation:** It uses square brackets for property access
4316+
2. **Square brackets notation:** It uses square brackets for property access
43034317

43044318
```javascript
43054319
objectName["property"];
43064320
```
43074321

4308-
1. **Expression notation:** It uses expression in the square brackets
4322+
3. **Expression notation:** It uses expression in the square brackets
43094323

43104324
```javascript
43114325
objectName[expression];
@@ -4427,7 +4441,9 @@
44274441
Synchronous iteration was introduced in ES6 and it works with below set of components,
44284442

44294443
**Iterable:** It is an object which can be iterated over via a method whose key is Symbol.iterator.
4444+
44304445
**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+
44314447
**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.
44324448

44334449
Let's demonstrate synchronous iteration with an array as below,
@@ -6058,7 +6074,9 @@
60586074
60596075
327. ### How do you avoid receiving postMessages from attackers
60606076
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),
60626080
60636081
```javascript
60646082
//Listener on http://www.some-receiver.com/
@@ -7084,6 +7102,7 @@
70847102
The main sources of microtasks are Promise.resolve, Promise.reject, MutationObservers, IntersectionObservers etc
70857103
70867104
**Note:** All of these microtasks are processed in the same turn of the event loop.
7105+
70877106
**[⬆ Back to Top](#table-of-contents)**
70887107
70897108
389. ### What are different event loops
@@ -7094,9 +7113,9 @@
70947113
2. The Node.js Event Loop
70957114
70967115
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.
70987117
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.
71007119
71017120
**[⬆ Back to Top](#table-of-contents)**
71027121
@@ -8102,25 +8121,25 @@
81028121
81038122
The _debounce()_ function can be used on input, button and window events.
81048123
8105-
**Input:**
8124+
**Input:**
81068125
8107-
```html
8108-
<input type="text" onkeyup="processChange()" />
8109-
```
8126+
```html
8127+
<input type="text" onkeyup="processChange()" />
8128+
```
81108129
8111-
**Button:**
8130+
**Button:**
81128131
8113-
```html
8114-
<button onclick="processChange()">Click me</button>
8115-
```
8132+
```html
8133+
<button onclick="processChange()">Click me</button>
8134+
```
81168135
8117-
**Windows event:**
8136+
**Windows event:**
81188137
8119-
```html
8120-
window.addEventListener("scroll", processChange);
8121-
```
8138+
```html
8139+
window.addEventListener("scroll", processChange);
8140+
```
81228141
8123-
**[⬆ Back to Top](#table-of-contents)**
8142+
**[⬆ Back to Top](#table-of-contents)**
81248143
81258144
435. ### What is throttling?
81268145
@@ -8423,42 +8442,43 @@ multiplyBy2(add(2, 3));
84238442
449. ### What is the purpose of the this keyword in JavaScript?
84248443
* 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.
84258444
* 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).
84318445
8432-
**Example 2: this in a Function**
8433-
```javascript
8434-
function displayThis() {
8446+
**Example 1: this in a Global Context**
8447+
```javascript
84358448
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).
84418451
8442-
**Example 3: this in a Method**
8443-
```javascript
8444-
const person = {
8445-
name: 'John',
8446-
greet: function() {
8447-
console.log('Hello, ' + this.name);
8452+
**Example 2: this in a Function**
8453+
```javascript
8454+
function displayThis() {
8455+
console.log(this);
84488456
}
8449-
};
84508457

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.
84548461
8455-
**Example 4: this in an Event Handler**
8456-
```javascript
8457-
document.getElementById('myButton').addEventListener('click', function() {
8458-
console.log(this);
8459-
});
8460-
```
8461-
* In an event handler, this refers to the element that triggered the event (the button in this case).
8462+
**Example 3: this in a Method**
8463+
```javascript
8464+
const person = {
8465+
name: 'John',
8466+
greet: function() {
8467+
console.log('Hello, ' + this.name);
8468+
}
8469+
};
8470+
8471+
person.greet();
8472+
```
8473+
* In a method, this refers to the object that owns the method (person in the case).
8474+
8475+
**Example 4: this in an Event Handler**
8476+
```javascript
8477+
document.getElementById('myButton').addEventListener('click', function() {
8478+
console.log(this);
8479+
});
8480+
```
8481+
* In an event handler, this refers to the element that triggered the event (the button in this case).
84628482
84638483
**[⬆ Back to Top](#table-of-contents)**
84648484
@@ -8475,6 +8495,7 @@ Here are some common use cases of closures:
84758495
* 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.
84768496
84778497
* 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+
84788499
**[⬆ Back to Top](#table-of-contents)**
84798500
84808501
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
85898610
const nums = [1, 2, 3, 4, 5, 6];
85908611
const evenNums = nums.myFilter(x => x % 2);
85918612
console.log(evenNums); // [2, 4, 6]
8592-
```
8613+
```
85938614
3. **reduce:**
85948615
85958616
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
87688789
87698790
**Note:** You should only use either async or defer attribute if the `src` attribute is present.
87708791
8771-
**[⬆ Back to Top](#table-of-contents)**
8792+
**[⬆ Back to Top](#table-of-contents)**
87728793
87738794
464. ### What is Lexical Scope?
87748795
87758796
Lexical scope is the ability for a function scope to access variables from the parent scope.
8776-
8797+
```js
87778798
<script>
87788799
function x(){
87798800
var a=10;
@@ -8785,6 +8806,7 @@ The execution context is created when a function is called. The function's code
87858806
}
87868807
x();
87878808
</script>
8809+
```
87888810
87898811
**[⬆ Back to Top](#table-of-contents)**
87908812
@@ -11795,6 +11817,35 @@ If a function is called with `undefined`, the `undefined` value is treated as a
1179511817
</details>
1179611818
1179711819
**[⬆ Back to Top](#table-of-contents)**
11820+
11821+
11822+
#### 86. What is the output of below code?
11823+
11824+
```javascript
11825+
function func(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.
11844+
11845+
</p>
11846+
</details>
11847+
11848+
**[⬆ Back to Top](#table-of-contents)**
1179811849
1179911850
## Disclaimer
1180011851

0 commit comments

Comments
 (0)