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
897

Showing posts older than #2516 Β· Back to latest

Older Posts 20 shown
Post #2515 4.6K
  • ❀ 3
  • πŸ‘ 2
  • πŸ”₯ 2
Post #2514 4.49K
CHALLENGE

function greet(name) {
return `Hello, ${name}!`;
}

function highlight(strings, ...values) {
return strings.reduce((result, str, i) => {
return result + str + (values[i] ? `<em>${values[i]}</em>` : '');
}, '');
}

const user = 'Sarah';
const status = 'online';

console.log(highlight`User ${user} is currently ${status}.`);
  • ❀ 2
  • πŸ”₯ 2
Post #2513 4.56K
  • πŸ‘ 5
  • ❀ 1
  • πŸ”₯ 1
Post #2512 4.79K
CHALLENGE

async function processValues() {
try {
console.log('Start');
const a = await Promise.resolve('First');
console.log(a);
const b = await Promise.reject('Error');
console.log(b);
return 'Done';
} catch (err) {
console.log(err);
return 'Recovered';
} finally {
console.log('Finally');
}
}

processValues().then(result => console.log(result));
  • ❀ 2
Post #2511 4.71K
πŸ˜‚
  • 🀣 65
  • πŸ”₯ 4
  • ❀ 2
  • πŸ‘ 2
Post #2510 5.02K
  • ❀ 8
  • πŸ‘ 1
Post #2509 4.85K
CHALLENGE

class LightMachine {
constructor() {
this.states = {
green: { next: 'yellow' },
yellow: { next: 'red' },
red: { next: 'green' }
};
this.currentState = 'green';
}

transition() {
this.currentState = this.states[this.currentState].next;
return this.currentState;
}
}

const lightMachine = new LightMachine();
let result = '';
for (let i = 0; i < 5; i++) {
result += lightMachine.transition() + ' ';
}
console.log(result.trim());
  • ❀ 4
  • πŸ‘ 1
Post #2508 4.12K
πŸ‘€ npq: Safely Install Packages by Auditing Them Pre-Install

npq performs several extra steps compared to npm. It consults Snyk’s database of vulnerabilities, looks at the package’s age, download count, and docs, and tries to paint a better picture of what you’re really installing.

Liran Tal
  • πŸ”₯ 5
  • πŸ‘ 4
  • ❀ 2
Post #2507 4.53K
  • πŸ”₯ 6
  • πŸ‘ 3
  • ❀ 2
Post #2506 4.49K
CHALLENGE

const weakSet = new WeakSet();

const obj1 = { name: 'First' };
const obj2 = { name: 'Second' };
const obj3 = obj1;

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

let result = '';
result += weakSet.has(obj1) + ', ';
result += weakSet.has(obj3) + ', ';

obj2.name = 'Modified';
result += weakSet.has(obj2) + ', ';

weakSet.delete(obj1);
result += weakSet.has(obj3);

console.log(result);
  • ❀ 8
  • πŸ”₯ 2
Post #2505 4.52K
🀨 Laravel and Node.js: PHP in Watt Runtime

In June we featured php-node, a new way to β€˜bridge the gap’ between PHP and Node.js by being able to embed PHP into Node apps. Now they’ve gone a step further by using php-node and the Watt app server to enable the running of Laravel apps too. A curious meeting of ecosystems!

Stephen Belanger (Platformatic)
  • ❀ 6
  • πŸ‘ 2
  • πŸ”₯ 1
  • πŸ€” 1
Post #2504 4.43K
  • πŸ€” 7
  • πŸ‘ 5
  • ❀ 1
  • πŸ”₯ 1
Post #2503 4.41K
CHALLENGE

function createCounter() {
let count = 0;

const counter = {
increment() {
count++;
return count;
},
getCount() {
return count;
}
};

return counter;
}

let c1 = createCounter();
c1.increment();
c1.increment();

let c2 = c1;
c1 = null;

console.log(c2.getCount());
  • ❀ 4
  • πŸ‘ 1
  • πŸ”₯ 1
Post #2501 4.43K
  • πŸ‘ 5
Post #2500 4.57K
CHALLENGE

function* createCounter() {
let count = 0;
while (true) {
const reset = yield ++count;
if (reset) {
count = 0;
}
}
}

const counter = createCounter();
console.log(counter.next().value);
console.log(counter.next().value);
console.log(counter.next(true).value);
console.log(counter.next().value);
  • ❀ 9
  • πŸ‘ 2
  • 🀩 1
Post #2499 4.67K
  • πŸ”₯ 3
  • πŸ‘ 2
  • πŸ€” 2
  • ❀ 1
Post #2498 4.63K
CHALLENGE

const scores = [85, 92, 78, 90];  
const student = {
name: 'Jordan',
grade: 'A',
...{ courses: ['Math', 'Science'] },
scores,
average: function() { return this.scores.reduce((a, b) => a + b) / this.scores.length }
};

const { name, ...details } = student;
const [first, ...rest] = scores;

console.log(details.scores[0], rest[0]);
  • ❀ 6
  • πŸ”₯ 2
Post #2496 4.67K
  • πŸ‘ 8
  • ❀ 5
  • πŸ”₯ 3
Post #2495 4.62K
CHALLENGE

function getOrder() {
console.log('1');

setTimeout(() => console.log('2'), 0);

Promise.resolve().then(() => {
console.log('3');
Promise.resolve().then(() => console.log('4'));
});

Promise.resolve().then(() => console.log('5'));

console.log('6');
}

getOrder();
  • ❀ 5
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 β†’