Skip to content

Instantly share code, notes, and snippets.

View ahamed's full-sized avatar
🎯
Focusing on open source.

Sajeeb Ahamed ahamed

🎯
Focusing on open source.
View GitHub Profile
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
#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
};
```
@ahamed
ahamed / useDebounce.ts
Created September 10, 2022 06:12
Implementation of the debounce hook for ReactJs.
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.
@ahamed
ahamed / groupBy.ts
Last active August 25, 2022 15:57
Implement the groupBy method using typescript.
/**
* 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
@ahamed
ahamed / array-from-array-like-object.js
Created August 18, 2022 06:55
Create an array using Array.from() static method from an array-like object.
/**
* 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 = {
@ahamed
ahamed / create-enum.js
Last active August 9, 2022 17:15
Create a JavaScript representation of "Enum".
/**
* ✅ 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]>
*
/**
* 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',
@ahamed
ahamed / indexOf.ts
Created July 21, 2022 16:47
Find an item's index from a tuple.
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
@ahamed
ahamed / extract-promise.ts
Created July 19, 2022 17:13
Extract [response, error] from a promise function.
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 {
@ahamed
ahamed / is-same-array.js
Last active July 17, 2022 15:09
Check if two arrays are same i.e. same elements but in different order.
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.