TGViewer
Channel Public Channel
JavaScript

JavaScript

@javascript

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
Subscribers
31.1K
Photos
1.2K
Videos
10
Links
899

Showing posts older than #2335 Β· Back to latest

Older Posts 20 shown
Post #2334 5.03K
CHALLENGE

function modify(obj) {
obj.a.push(4);
obj.b = 'changed';
return obj;
}

const original = { a: [1, 2, 3], b: 'original', c: { deep: true } };
const copy1 = { ...original };
const copy2 = JSON.parse(JSON.stringify(original));

modify(copy1);
console.log(original.a, original.b, copy2.c === original.c);
  • πŸ‘ 5
  • ❀ 1
  • 🀩 1
Post #2333 5.11K
  • πŸ‘ 10
  • ❀ 2
  • πŸ”₯ 1
  • 🀩 1
Post #2332 5.09K
CHALLENGE

function processTransaction(amount) {
try {
if (typeof amount !== 'number') {
throw new TypeError('Amount must be a number');
}
if (amount <= 0) {
throw new RangeError('Amount must be positive');
}
return 'Transaction processed';
} catch (error) {
if (error instanceof TypeError) {
return { status: 'Type Error', message: error.message };
} else if (error instanceof RangeError) {
return { status: 'Range Error', message: error.message };
}
return { status: 'Unknown Error', message: error.message };
}
}

console.log(processTransaction(-50));
  • πŸ‘ 4
  • ❀ 1
  • 🀩 1
Post #2331 4.89K
  • πŸ”₯ 5
  • ❀ 1
  • πŸ‘ 1
  • 🀩 1
Post #2330 4.96K
CHALLENGE

const user = { name: 'Alice' };
const ratings = new WeakMap();

ratings.set(user, 5);
const result = [];

result.push(ratings.has(user));
result.push(ratings.get(user));

// Create a reference-free object
let tempUser = { name: 'Bob' };
ratings.set(tempUser, 10);
result.push(ratings.has(tempUser));

// Remove the reference
tempUser = null;

// Try to iterate through WeakMap
result.push(typeof ratings[Symbol.iterator]);

console.log(result);
  • πŸ”₯ 5
  • ❀ 1
Post #2329 5.09K
  • πŸ‘ 6
  • ❀ 3
  • πŸ”₯ 1
Post #2328 5.35K
CHALLENGE

console.log(1);

setTimeout(() => {
console.log(2);
Promise.resolve().then(() => console.log(3));
}, 0);

Promise.resolve()
.then(() => {
console.log(4);
setTimeout(() => console.log(5), 0);
})
.then(() => console.log(6));

console.log(7);
  • πŸ‘ 2
  • ❀ 1
  • πŸ€” 1
  • 🀩 1
Post #2326 5.44K
  • ❀ 11
  • πŸ‘ 3
  • πŸ”₯ 2
  • πŸ€” 1
Post #2325 4.99K
CHALLENGE

const weakSet = new WeakSet();

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

weakSet.add(obj1);
weakSet.add(obj2);

const results = [
weakSet.has(obj1),
weakSet.has(obj3),
weakSet.has({ id: 2 }),
weakSet.has(obj2)
];

obj1 = null;

console.log(results);
  • ❀ 3
  • πŸ‘ 1
  • 🀩 1
Post #2324 4.99K
πŸ˜†
  • 🀣 34
  • πŸ€” 5
  • πŸ‘ 4
Post #2323 5.43K
  • πŸ‘ 4
  • πŸ€” 4
Post #2321 5.26K
CHALLENGE

const user = {
name: "Alice",
age: 32,
role: "developer"
};

const handler = {
get(target, prop) {
return prop in target ?
`Value: ${target[prop]}` :
"Not found";
}
};

const proxy = new Proxy(user, handler);
delete user.age;

console.log(Reflect.get(proxy, "name") + ", " + proxy.age + ", " + proxy.skills);
  • πŸ‘ 4
Post #2319 5.36K
  • ❀ 9
  • πŸ‘ 3
  • πŸ”₯ 2
  • 🀣 2
Post #2318 5.02K
CHALLENGE

const team = {
name: 'Eagles',
players: ['Smith', 'Johnson', 'Williams'],
coach: { name: 'Brown', experience: 12 },
stats: { wins: 10, losses: 6 }
};

const {
name: teamName,
players: [firstPlayer, , thirdPlayer],
coach: { name },
stats: { wins, draws = 0 }
} = team;

console.log(`${teamName}-${firstPlayer}-${thirdPlayer}-${name}-${wins}-${draws}`);
  • πŸ‘ 5
  • 🀣 5
  • ❀ 1
Post #2317 5.15K
  • πŸ‘ 7
  • ❀ 3
  • πŸ€” 2
Post #2316 5.11K
CHALLENGE

const a = 9007199254740991n; // MAX_SAFE_INTEGER as BigInt
const b = 2n;
const c = a + b;

const result = [
a === 9007199254740991,
a + 1n === 9007199254740992n,
typeof c,
c > Number.MAX_SAFE_INTEGER,
BigInt(9007199254740992) - BigInt(9007199254740991)
];

console.log(result);
  • 🀣 4
  • ❀ 3
  • πŸ€” 3
  • πŸ‘ 2
Post #2315 5.18K
🀟 Node 24 (Current) Released

Node’s release lines are shifting a little lately – v18 has gone EOL and now v23 gives way to v24 as the β€˜Current’ release for when you need the cutting edge features. It comes with npm 11, V8 13.6 (hello RegExp.escape, Float16Array, and `Error.isError`), the URLPattern API exposed by default, plus Undici 7.

Node.js Team
  • πŸ‘ 12
  • ❀ 1
  • πŸ”₯ 1
Post #2314 4.86K
  • πŸ‘ 10
  • ❀ 4
Older posts β†’
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook β†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 β†’