This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hoisting in JavaScript is a behavior where variable and function declarations are moved to the top of their containing scope during the compile phase. This means that regardless of where declarations occur within a scope, they are hoisted to the top. However, it's important to note that only declarations are hoisted, not initializations or assignments. Let me show you an example to illustrate this: | |
```js | |
console.log(myVar); // Outputs: undefined | |
var myVar = 10; | |
console.log(myVar); // Outputs: 10 | |
``` | |
In this example, even though `myVar` is logged before it's declared and initialized, it doesn't throw an error. This is because the declaration `var myVar;` is hoisted to the top of its scope during the compile phase, resulting in `myVar` being `undefined` when logged before its assignment. | |
For that reason we could call a function before of its declaration. For example- | |
```js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1: | |
## Prototype: | |
An object is a data structure used in JavaScript that stores values in a key value pair. That means you can store a value into an object in a key. For example- | |
```js | |
const person = { | |
name: 'John Doe', | |
age: 20 | |
}; | |
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from 'react'; | |
/** | |
* useDebounce hook for implementing debounce in react. | |
* The idea is simple, keep an internal state and update | |
* the state after a specific delay time. If the given value | |
* is changed before the delay time then cancel the timeout and | |
* wait for the delay time once again. And finally return the debounced | |
* state value. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JavaScript introduces a new array method name `group`, but this is only added to the firefox nightly build. | |
* Here we can create the similar implementation of the `Array.prototype.group` method. | |
* | |
* The `Array.prototype.group` method basically accespts a callback function as a parameter and group the array elements | |
* according to the callback function's return key and return an object. | |
* From the callback function you have to return a string or a number. | |
* | |
* @author Sajeeb Ahamed <[email protected]> | |
* @see https://www.typescriptlang.org/play?#code/PQKhCgAIUgpBDAbvAygYwE4EsAOAXSLAOzwwHsATAVzQFMBnSeSI2gdyYw3gE9IBbWngAWlFvEGQABgHNyVHFIA0kAEZUCIrI22QyRADZ94FCrQqQ8ZS8NqQAZlgy17ZAB4ssM4XiNqqWAYUAHRQMAAStM6QbHZo8ESQmLTweHYidvRY-IHwGIT8OAa0giSpWPp69jZ2UgCCXLzBOORWeDw4tMFyZApSAkKiIWFhkAAqttIN3DzNrWTtnd3yigMiYqrwWfEGfvBodPT4jMw7BptoANYOVERoeBWJW0yQOHkSQlFMRBY9CjWcGaQWjFUp4eijfZoMgYCjEGSWawZJLwXYXa72W73R4AckYzjwVAwiUutGMP0gBKJT0SZFUACtaPdQtBIAAxcj8AFndE3O4PSo8XqQYRIdLWKnEl70UjwvT5ZhEKj8VRRFkQVkAAXg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Create an array from an array-like object. | |
* An array-like object is an object which has a length property | |
* and indexed elements. An indexed element means the properties | |
* of the object are like the index of an array. | |
* | |
* @author Sajeeb Ahamed <[email protected]> | |
*/ | |
const arrayLikeObject = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ✅ Function for creating an `enum` representation in JavaScript. | |
* | |
* This function will receive a plain object | |
* [key: string]: number | string | boolean. | |
* You've created the enum with a valid object and it will return you a | |
* Proxy. You are not able to extend the proxy anymore. | |
* | |
* @author 📩 Sajeeb Ahamed <[email protected]> | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Remove some properties from an object, | |
* and work with other properties. | |
* | |
* You can use destructuring and ...rest operator for gaining this result. | |
* | |
* @author Sajeeb Ahamed <[email protected]> | |
*/ | |
const store = { | |
id: '123', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type IncrementByOne<N extends number, T extends unknown[] = []> = | |
T['length'] extends N | |
? [...T, unknown]['length'] | |
: IncrementByOne<N, [...T, unknown]>; | |
type IndexOf<T extends unknown[], U extends unknown, I extends number = 0> = | |
I extends T['length'] | |
? -1 | |
: T[I] extends U | |
? I |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const extractPromise = async < | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
F extends (...args: any) => Promise<any>, | |
A extends Parameters<F>, | |
R extends ReturnType<F>, | |
>( | |
promiseFn: F, | |
...args: A | |
): Promise<[Awaited<R> | undefined, unknown | undefined]> => { | |
try { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr1 = [1, 2, 3, 4, 6, 5, 2]; | |
const arr2 = [2, 3, 1, 4, 2, 5, 6]; | |
const isSameArray = (arr1, arr2) => { | |
if (arr1.length !== arr2.length) { | |
return false; | |
} | |
// Create object like {1: 1, 2: 2, 3: 1 ...} , the number of frequency of an element in the array. |
NewerOlder