Skip to content

Commit 5b320fb

Browse files
Mayur KadamMayur Kadam
Mayur Kadam
authored and
Mayur Kadam
committed
segregated files with topics
1 parent f1c49b1 commit 5b320fb

File tree

96 files changed

+1553
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1553
-0
lines changed

Diff for: .DS_Store

6 KB
Binary file not shown.

Diff for: 01 - JavaScript & TypeScript Basic/.DS_Store

6 KB
Binary file not shown.
6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# JavaScript Iterators and Iterables
2+
3+
**Full Article:** [Iterators and Iterables](https://medium.com/mighty-ghost-hack/js-interview-10-iterators-and-iterables-227827118b20)
4+
5+
## 🔹 Introduction
6+
In JavaScript, **iterables** are objects that implement the **`Symbol.iterator`** method, making them compatible with iteration protocols. **Iterators** are objects that define a **`next()`** method, which returns an object with `{ value, done }` properties.
7+
8+
---
9+
10+
## 📌 Iterables in JavaScript
11+
An **iterable** is an object that provides a way to iterate over its elements using a loop. Common iterables include:
12+
13+
- Arrays
14+
- Strings
15+
- Maps
16+
- Sets
17+
18+
### ✅ Example: Built-in Iterables
19+
```js
20+
const arr = [1, 2, 3];
21+
for (let num of arr) {
22+
console.log(num);
23+
}
24+
// Output: 1, 2, 3
25+
```
26+
27+
### ✅ Checking If an Object Is Iterable
28+
```js
29+
console.log(typeof arr[Symbol.iterator]); // Output: function
30+
```
31+
32+
---
33+
34+
## 📌 Iterators in JavaScript
35+
An **iterator** is an object with a **`next()`** method that returns `{ value, done }`:
36+
37+
### ✅ Example: Creating an Iterator Manually
38+
```js
39+
function createIterator(arr) {
40+
let index = 0;
41+
return {
42+
next: function() {
43+
return index < arr.length
44+
? { value: arr[index++], done: false }
45+
: { value: undefined, done: true };
46+
}
47+
};
48+
}
49+
50+
const iterator = createIterator(["a", "b", "c"]);
51+
console.log(iterator.next()); // { value: "a", done: false }
52+
console.log(iterator.next()); // { value: "b", done: false }
53+
console.log(iterator.next()); // { value: "c", done: false }
54+
console.log(iterator.next()); // { value: undefined, done: true }
55+
```
56+
57+
---
58+
59+
## 📌 Custom Iterable Objects
60+
You can make any object iterable by implementing the **`Symbol.iterator`** method:
61+
62+
### ✅ Example: Custom Iterable Object
63+
```js
64+
const myIterable = {
65+
data: [10, 20, 30],
66+
[Symbol.iterator]: function() {
67+
let index = 0;
68+
return {
69+
next: () => {
70+
return index < this.data.length
71+
? { value: this.data[index++], done: false }
72+
: { done: true };
73+
}
74+
};
75+
}
76+
};
77+
78+
for (let value of myIterable) {
79+
console.log(value);
80+
}
81+
// Output: 10, 20, 30
82+
```
83+
84+
---
85+
86+
## 📌 Generators as Iterators
87+
**Generators** simplify iterator creation using the `function*` syntax:
88+
89+
### ✅ Example: Generator Function
90+
```js
91+
function* generatorFunc() {
92+
yield 1;
93+
yield 2;
94+
yield 3;
95+
}
96+
97+
const gen = generatorFunc();
98+
console.log(gen.next()); // { value: 1, done: false }
99+
console.log(gen.next()); // { value: 2, done: false }
100+
console.log(gen.next()); // { value: 3, done: false }
101+
console.log(gen.next()); // { value: undefined, done: true }
102+
```
103+
104+
---
105+
106+
## 📌 Practical Use Cases
107+
**Iterating Over Data**
108+
```js
109+
const set = new Set(["apple", "banana", "cherry"]);
110+
for (let item of set) {
111+
console.log(item);
112+
}
113+
// Output: apple, banana, cherry
114+
```
115+
116+
**Spreading Iterables**
117+
```js
118+
const str = "hello";
119+
const letters = [...str];
120+
console.log(letters); // [ 'h', 'e', 'l', 'l', 'o' ]
121+
```
122+
123+
**Custom Range Iterator**
124+
```js
125+
function range(start, end) {
126+
return {
127+
[Symbol.iterator]: function() {
128+
let current = start;
129+
return {
130+
next: () => {
131+
return current <= end
132+
? { value: current++, done: false }
133+
: { done: true };
134+
}
135+
};
136+
}
137+
};
138+
}
139+
140+
for (let num of range(1, 5)) {
141+
console.log(num);
142+
}
143+
// Output: 1, 2, 3, 4, 5
144+
```
145+
146+
---
147+
148+
## 🔹 Summary
149+
- **Iterables** implement `Symbol.iterator`.
150+
- **Iterators** have a `next()` method returning `{ value, done }`.
151+
- **Generators** simplify iteration.
152+
- Iterables allow use in `for...of`, spreading (`...`), and `Array.from()`.
153+
154+
🚀 **Mastering iterators and iterables helps in handling asynchronous data and optimizing JavaScript performance!**
155+

Diff for: 01 - JavaScript & TypeScript Basic/JavaScript/NaN.md

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Introduction to JavaScript NaN
2+
3+
**Full Article:** [JavaScript NaN](https://medium.com/mighty-ghost-hack/js-interview-6-introduction-to-javascript-nan-d6b1a441e7a8)
4+
5+
## What is `NaN`?
6+
`NaN` stands for **Not-a-Number**. It is a special value in JavaScript that represents an invalid number or an unrepresentable mathematical operation.
7+
8+
```javascript
9+
console.log(typeof NaN); // "number"
10+
```
11+
Even though `NaN` means "Not-a-Number," its type is still `number` in JavaScript.
12+
13+
---
14+
15+
## When does `NaN` occur?
16+
### 1️⃣ Invalid Mathematical Operations
17+
```javascript
18+
console.log(0 / 0); // NaN
19+
console.log(Infinity - Infinity); // NaN
20+
console.log(Math.sqrt(-1)); // NaN
21+
```
22+
23+
### 2️⃣ Attempting to Convert Non-Numeric Values
24+
```javascript
25+
console.log(parseInt("hello")); // NaN
26+
console.log(Number("world")); // NaN
27+
console.log(5 * "abc"); // NaN
28+
```
29+
30+
### 3️⃣ Operations with `undefined`
31+
```javascript
32+
let x;
33+
console.log(x + 2); // NaN
34+
```
35+
36+
---
37+
38+
## How to Check for `NaN`?
39+
### 1️⃣ Using `isNaN()` (Global Function)
40+
```javascript
41+
console.log(isNaN("hello")); // true
42+
console.log(isNaN(42)); // false
43+
console.log(isNaN(NaN)); // true
44+
```
45+
🔹 **Caution:** `isNaN()` performs type coercion, which may lead to unexpected results.
46+
```javascript
47+
console.log(isNaN("123")); // false ("123" is converted to a number)
48+
console.log(isNaN("123abc")); // true (cannot fully convert)
49+
```
50+
51+
### 2️⃣ Using `Number.isNaN()` (Safer Approach)
52+
```javascript
53+
console.log(Number.isNaN(NaN)); // true
54+
console.log(Number.isNaN("hello")); // false
55+
console.log(Number.isNaN(42)); // false
56+
```
57+
`Number.isNaN()` does **not** perform type coercion, making it a more reliable check.
58+
59+
---
60+
61+
## `NaN` Behavior in Arithmetic Operations
62+
### 1️⃣ Propagation of `NaN`
63+
Once `NaN` appears in an operation, it usually propagates:
64+
```javascript
65+
console.log(NaN + 5); // NaN
66+
console.log(NaN * 10); // NaN
67+
```
68+
69+
### 2️⃣ `NaN` is Not Equal to Itself
70+
```javascript
71+
console.log(NaN === NaN); // false
72+
```
73+
🔹 This is because `NaN` is **not** equal to anything, even itself!
74+
75+
To check for `NaN`, use:
76+
```javascript
77+
console.log(Object.is(NaN, NaN)); // true
78+
```
79+
80+
---
81+
82+
## Fixing `NaN` Issues
83+
### 1️⃣ Provide Default Values with `||` or `??`
84+
```javascript
85+
let result = NaN || 0; // 0 (using OR operator)
86+
let safeValue = NaN ?? 10; // 10 (Nullish coalescing operator, works only for null & undefined)
87+
```
88+
89+
### 2️⃣ Validate Input Before Performing Operations
90+
```javascript
91+
function safeDivide(a, b) {
92+
if (typeof a !== "number" || typeof b !== "number" || b === 0) {
93+
return "Invalid Operation";
94+
}
95+
return a / b;
96+
}
97+
98+
console.log(safeDivide(10, 2)); // 5
99+
console.log(safeDivide(10, 0)); // "Invalid Operation"
100+
console.log(safeDivide(10, "hello")); // "Invalid Operation"
101+
```
102+
103+
---
104+
105+
## Summary
106+
- `NaN` stands for "Not-a-Number" and occurs due to invalid math operations or failed conversions.
107+
- `isNaN()` is **not** always reliable due to type coercion; prefer `Number.isNaN()`.
108+
- `NaN` is **not equal** to itself, making direct comparisons unreliable.
109+
- Use proper validation and default values to prevent `NaN` issues in code.
110+
111+
🚀 **Understanding `NaN` helps in writing more robust JavaScript applications!**
112+

0 commit comments

Comments
 (0)