JavaScript
31.9K subscribers
1.01K photos
9 videos
33 files
691 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
CHALLENGE

function mystery() {
try {
throw new Error('Oops');
return 'A';
} catch (err) {
return 'B';
} finally {
return 'C';
}
}

function wrapper() {
try {
return mystery();
} finally {
console.log('D');
}
}

console.log(wrapper());
πŸ‘7
What is the output?
Anonymous Quiz
32%
C D
37%
B D C
20%
D C
11%
D B
πŸ”₯3❀2πŸ‘2
CHALLENGE

function compose(...funcs) {
return function(x) {
return funcs.reduceRight((acc, fn) => fn(acc), x);
};
}

const add10 = x => x + 10;
const multiply2 = x => x * 2;
const toString = x => `Result: ${x}`;

const composed = compose(toString, multiply2, add10);
console.log(composed(5));
πŸ‘6❀1
πŸ‘8πŸ€”4❀2πŸ”₯2
CHALLENGE

try {
try {
// Creating a custom error type
class CustomError extends Error {
constructor(message) {
super(message);
this.name = "CustomError";
}
}

const fn = () => {
throw new CustomError("Something went wrong");
};

try {
fn();
} catch (e) {
throw new TypeError("Type error occurred");
}
} catch (e) {
console.log(e instanceof Error, e.name, e instanceof TypeError);
}
} catch (e) {
console.log("Outer catch", e.message);
}
❀5πŸ‘2πŸ”₯1
❀3πŸ‘3πŸ”₯2
CHALLENGE

function* counter() {
let i = 1;
while (true) {
const reset = yield i++;
if (reset) {
i = 1;
}
}
}

const gen = counter();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next(true).value);
console.log(gen.next().value);
❀2
πŸ‘8❀2
CHALLENGE

const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
];

const userScores = new WeakMap();

// Set scores for users
userScores.set(users[0], 95);
userScores.set(users[1], 80);

// Remove reference to Bob
users[1] = null;

let sum = 0;
for (const user of users) {
if (user && userScores.has(user)) {
sum += userScores.get(user);
}
}

console.log(sum);
πŸ‘7❀2
❀4πŸ”₯1
😭 Matteo Collina has unveiled php-node, a way to run PHP apps within the same process as Node.js. Run WordPress with a Next.js frontend? Sure.
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ€”11🀣8πŸ‘4
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());
❀3
🀩 Wake Up, Remix! Everything's Changing..

Big news from the Remix camp this week. About a year ago, Remix and React Router merged together reflecting their shared goals and code, but now it’s all change again. React Router is now basically what Remix originally intended to be, and so β€˜Remix’ is rebooting as a model-first, low-dependency, Web API-centric full-stack framework built on Preact. It’ll no longer be a 'React framework' per se.

Michael Jackson and Ryan Florence
Please open Telegram to view this post
VIEW IN TELEGRAM
❀5πŸ‘1πŸ”₯1
CHALLENGE

const team = {
lead: { name: 'Alice', projects: ['Alpha', 'Beta'] },
dev: { name: 'Bob', projects: ['Gamma'] },
tester: { name: 'Charlie', projects: [] }
};

const {
lead: { projects: [leadProject] },
dev: { projects: [devProject = 'Delta'] },
tester: { projects: [testerProject = 'Epsilon'] }
} = team;

console.log(`${leadProject}-${devProject}-${testerProject}`);
❀3
πŸ‘6❀5πŸ€”2🀣2
🟠 Svelte Flow 1.0: Create Node-Based UIs and Diagrams in Svelte

From the creators of the similar React Flow comes a customizable Svelte component for building node-based editors and interactive diagrams. Want examples?

webkid GmbH
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘2πŸ”₯2❀1
CHALLENGE

const target = { a: 1, b: 2 };
const handler = {
get(obj, prop) {
return prop in obj ? obj[prop] * 2 : 'not found';
},
set(obj, prop, value) {
if (typeof value !== 'number') {
return false;
}
obj[prop] = value + 10;
return true;
}
};

const proxy = new Proxy(target, handler);
proxy.c = '5';
proxy.d = 5;

console.log(JSON.stringify({
a: proxy.a,
b: proxy.b,
c: proxy.c,
d: target.d,
hasC: Reflect.has(target, 'c')
}));
πŸ€”6❀1πŸ”₯1
CHALLENGE

function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
return function(...moreArgs) {
return curried.apply(this, [...args, ...moreArgs]);
};
};
}

const multiply = curry((a, b, c) => a * b * c);
const double = multiply(2);
const result = double(3)(4);

console.log(result);
πŸ”₯3