JavaScript
31.9K subscribers
1.01K photos
9 videos
33 files
693 links
A resourceful newsletter featuring the latest and most important news, articles, books and updates in the world of #javascript ๐Ÿš€ Don't miss our Quizzes!

Let's chat: @nairihar
Download Telegram
๐Ÿ‘8๐Ÿค”7โค3๐Ÿ”ฅ1
CHALLENGE

type User = {
id: number;
name: string;
role?: 'admin' | 'user';
};

function processUser(user: Partial<User>): string {
const defaultUser: User = {
id: 0,
name: 'Guest',
role: 'user'
};

const mergedUser = { ...defaultUser, ...user };

if (mergedUser.role === 'admin') {
return `Admin: ${mergedUser.name}`;
}

return `User: ${mergedUser.name} (ID: ${mergedUser.id})`;
}

console.log(processUser({ name: 'John', role: 'admin' }));
๐Ÿ‘7โค1
๐Ÿ”ฅ6๐Ÿ‘4โค2
๐Ÿ‘12โค4๐Ÿค”4
CHALLENGE

function processConfig(config) {
const cache = config.cache ?? true;
const timeout = config.timeout ?? 1000;
const retries = config.retries ?? 3;

return {
useCache: cache,
timeoutMs: timeout,
maxRetries: retries
};
}

const result = processConfig({ timeout: 0, retries: false });
console.log(result);
๐Ÿ‘3
CHALLENGE

function processInput(userInput) {
const defaultValue = 'default';
const value1 = userInput?.value ?? defaultValue;
const value2 = userInput?.value || defaultValue;

const result = {
a: 0 ?? 'zero',
b: '' ?? 'empty',
c: null ?? 'null',
d: undefined ?? 'undefined',
comparison: value1 === value2
};

console.log(result);
}

processInput({ value: '' });
๐Ÿ‘7
๐Ÿ‘8โค2๐Ÿ”ฅ2๐Ÿคฃ2๐Ÿค”1
CHALLENGE

class ShoppingCart {
constructor() {
if (ShoppingCart.instance) {
return ShoppingCart.instance;
}

this.items = [];
ShoppingCart.instance = this;
}

addItem(item) {
this.items.push(item);
}

getItems() {
return [...this.items];
}
}

const cart1 = new ShoppingCart();
const cart2 = new ShoppingCart();

cart1.addItem('Book');
cart2.addItem('Laptop');

console.log(cart1.getItems());
๐Ÿ‘6
๐Ÿ‘10๐Ÿค”4โค2๐Ÿ”ฅ2๐Ÿคฉ1
๐Ÿซก Teable: Open Source Airtable Alternative atop Postgres

Airtable is a popular data table database SaaS, but hereโ€™s a NestJS-powered open-source alternative in a similar manner that sits atop Postgres. GitHub repo.

Teable Team
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ‘4โค2๐Ÿ”ฅ2๐Ÿคฃ1
CHALLENGE

function Device(name) {
this.name = name;
this.isOn = false;
}

Device.prototype.turnOn = function() {
this.isOn = true;
return `${this.name} is now on`;
};

function Smartphone(name, model) {
Device.call(this, name);
this.model = model;
}

Smartphone.prototype = Object.create(Device.prototype);
Smartphone.prototype.constructor = Smartphone;

Smartphone.prototype.turnOn = function() {
const result = Device.prototype.turnOn.call(this);
return `${result} (model: ${this.model})`;
};

const myPhone = new Smartphone('iPhone', '13 Pro');
console.log(myPhone.turnOn());
๐Ÿ”ฅ7โค2๐Ÿ‘2๐Ÿคฃ1
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ‘4โค1๐Ÿ”ฅ1
CHALLENGE

let obj1 = { id: 1 };
let obj2 = { id: 2 };
let obj3 = { id: 3 };

const weakSet = new WeakSet([obj1, obj2]);

weakSet.add(obj3);
weakSet.delete(obj1);

obj2 = null;

const remainingObjects = [...weakSet];

console.log(remainingObjects);
๐Ÿ‘2๐Ÿ”ฅ1
๐Ÿ‘9๐Ÿ”ฅ5โค1
๐Ÿฅณ Next.js Global Hackathon

- 500 teams
- 10 days


Next.js team
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ”ฅ9๐Ÿ‘2
CHALLENGE

const secretData = { password: 'abc123' };
const mySet = new WeakSet();
mySet.add(secretData);

// Later in the code
delete secretData.password;

const checkAccess = (obj) => {
console.log(mySet.has(obj));
};

checkAccess(secretData);
checkAccess({ password: 'abc123' });
โค7๐Ÿ‘2๐Ÿ”ฅ2
๐Ÿ‘3๐Ÿ”ฅ2โค1