JavaScript
33K subscribers
1.14K photos
10 videos
33 files
814 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
What is the output?
Anonymous Quiz
23%
450.00
30%
500.00
15%
385.00
32%
495.00
โค4๐Ÿ”ฅ1๐Ÿค”1
CHALLENGE

class AppError extends Error {
constructor(message, code) {
super(message);
this.name = "AppError";
this.code = code;
}
}

function riskyOperation(value) {
if (value === null) throw new AppError("Null value", 404);
if (typeof value !== "number") throw new TypeError("Not a number");
if (value < 0) throw new RangeError("Negative value");
return value * 2;
}

const inputs = [42, null, "hello", -5];
const results = inputs.map((input) => {
try {
return riskyOperation(input);
} catch (err) {
if (err instanceof AppError) return `AppError:${err.code}`;
if (err instanceof TypeError) return `TypeError`;
if (err instanceof RangeError) return `RangeError`;
return `UnknownError`;
}
});

console.log(results);
โค2๐Ÿ‘2๐Ÿ”ฅ1
๐Ÿ‘€ Solid v2.0.0 Beta: The <Suspense> is Over

After a long experimental phase, Solid 2.0โ€™s first beta lands with first-class async support where computations can return Promises or async iterables, and the reactive graph suspends and resumes around them natively. <Suspense> is retired in favor of <Loading> for initial renders, and mutations get a first-class action() primitive with optimistic support. For existing users the breaking changes are substantial, but thereโ€™s a migration guide.

Ryan Carniato
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ‘5โค4๐Ÿ”ฅ4
CHALLENGE

const inventory = {
apples: 5,
bananas: 12,
cherries: 0,
dates: 8,
};

const result = Object.entries(inventory)
.filter(([_, qty]) => qty > 0)
.reduce((acc, [fruit, qty]) => {
acc[fruit] = qty * 2;
return acc;
}, {});

const keys = Object.keys(result);
const values = Object.values(result);

console.log(keys.length, values.reduce((sum, v) => sum + v, 0));
๐Ÿ‘5โค2๐Ÿ”ฅ2
What is the output?
Anonymous Quiz
21%
4 50
28%
3 60
19%
4 60
33%
3 50
100๐Ÿ‘5โค1
CHALLENGE


const handler = {
get(target, prop, receiver) {
if (prop === 'fullName') {
return `${Reflect.get(target, 'firstName', receiver)} ${Reflect.get(target, 'lastName', receiver)}`;
}
return Reflect.get(target, prop, receiver);
},
set(target, prop, value, receiver) {
if (typeof value !== 'string') {
return false;
}
return Reflect.set(target, prop, value.trim(), receiver);
},
has(target, prop) {
return prop.startsWith('_') ? false : Reflect.has(target, prop);
}
};

const person = new Proxy({ firstName: ' Clara', lastName: 'Oswald ', _secret: 'hidden' }, handler);
person.firstName = ' Clara';
person.lastName = ' Oswald';

console.log(person.fullName);
console.log('_secret' in person);
console.log(Reflect.ownKeys(person).length);
๐Ÿ”ฅ7โค4๐Ÿ‘2๐Ÿค”2
๐Ÿ”ฅ4๐Ÿ‘2โค1
CHALLENGE


const str = "JavaScript is Awesome!";

const result = str
.split(" ")
.map((word, i) => {
if (i % 2 === 0) return word.toUpperCase();
return word.toLowerCase();
})
.map((word) => [...word].reverse().join(""))
.join("-");

console.log(result);
โค8๐Ÿ‘2๐Ÿ”ฅ1
CHALLENGE


const person = { name: "Carlos", scores: [10, 20, 30] };
const clone = { ...person };

clone.name = "Diana";
clone.scores.push(40);

const snapshot = Object.assign({}, person);
snapshot.name = "Elena";
snapshot.scores.push(50);

console.log(person.name);
console.log(person.scores.length);
console.log(clone.name);
console.log(clone.scores === person.scores);
๐Ÿ”ฅ3๐Ÿ‘2โค1
โค2๐Ÿ‘2๐Ÿค”1
CHALLENGE

"use strict";

function createCounter() {
let count = 0;

return {
increment() { count++; },
decrement() { count--; },
getCount() { return count; },
reset: () => { count = 0; }
};
}

const counter = createCounter();
counter.increment();
counter.increment();
counter.increment();
counter.decrement();

const { getCount, reset } = counter;

console.log(getCount());
reset();
console.log(counter.getCount());
๐Ÿ”ฅ8โค1
CHALLENGE


const p1 = new Promise((resolve) => {
console.log("A");
resolve("X");
});

const p2 = p1.then((val) => {
console.log("B");
return val + "Y";
});

const p3 = p2.then((val) => {
console.log("C:", val);
});

console.log("D");
๐Ÿ‘3โค2๐Ÿ”ฅ2
โค6๐Ÿ‘5๐Ÿ”ฅ1๐Ÿค”1
CHALLENGE



class AppError extends Error {
constructor(message, statusCode) {
super(message);
this.name = this.constructor.name;
this.statusCode = statusCode;
}
}

class ValidationError extends AppError {
constructor(message) {
super(message, 400);
this.fields = [];
}
}

function riskyOperation(value) {
if (value === null) throw new ValidationError("Null value");
if (value < 0) throw new AppError("Negative value", 422);
return value * 2;
}

const results = [];

for (const val of [10, null, -5, 3]) {
try {
results.push(riskyOperation(val));
} catch (e) {
if (e instanceof ValidationError) {
results.push(`Validation:${e.statusCode}`);
} else if (e instanceof AppError) {
results.push(`App:${e.statusCode}`);
} else {
results.push("Unknown");
}
}
}

console.log(results.join(" | "));
๐Ÿ‘2๐Ÿคฉ2๐Ÿ”ฅ1
โœŒ๏ธ Temporal: The 9-Year Journey to Fix Time in JavaScript

JavaScriptโ€™s date/time handling is notoriously messy and libraries like Moment.js became popular as a way to work around it. In 2017, Maggie Johnson-Pint, a maintainer of Moment.js, proposed the Temporal API to fix date/time handling for good, and weโ€™re mostly there (support is growing, with Safari and Node to catch up).

Jason Williams (Bloomberg)
Please open Telegram to view this post
VIEW IN TELEGRAM
โค3
CHALLENGE


const p1 = new Promise((resolve) => {
console.log("A");
resolve("B");
});

const p2 = p1.then((val) => {
console.log(val);
return "C";
});

p2.then((val) => {
console.log(val);
});

console.log("D");
๐Ÿ”ฅ2โค1๐Ÿ‘1