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
895

Showing posts older than #2921 · Back to latest

Older Posts 20 shown
Post #2920 3.76K
CHALLENGE

const obj = {
name: 'Calculator',
values: [1, 2, 3, 4, 5],

regular: function() {
return this.values.filter(function(x) {
return x > 2;
}).length;
},

arrow: function() {
return this.values.filter(x => x > 2).length;
},

broken: () => {
return this.values.filter(x => x > 2).length;
}
};

console.log(obj.regular());
console.log(obj.arrow());
try {
console.log(obj.broken());
} catch(e) {
console.log('Error');
}
  • 👍 6
  • ❤ 3
  • 🔥 2
Post #2919 3.3K
  • ❤ 10
  • 👍 2
Post #2918 3.48K
CHALLENGE

const user = { name: 'Sarah', age: 0 };
const config = { theme: '', debug: false };

const result1 = user.age || 25;
const result2 = user.name && user.name.toUpperCase();
const result3 = config.theme || 'light';
const result4 = config.debug && console.log('Debug mode') || 'disabled';

console.log(result1);
console.log(result2);
console.log(result3);
console.log(result4);
  • 👍 6
  • ❤ 4
  • 🔥 3
  • 🤔 3
Post #2917 3.5K
✌️ Web Dependencies are Broken; Can We Fix Them?

Lea, who has worked at the heart of Web Standards for years, delivers a compelling (and educational) call to action about a problem every JavaScript developer has encountered: why is managing dependencies and introducing them into code so unnecessarily messy and what could we do about it?

Lea Verou
  • ❤ 7
  • 👍 5
Post #2916 3.25K
  • 🔥 7
  • ❤ 5
  • 👍 2
Post #2915 3.5K
CHALLENGE

const pipe = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value);

const add = (x) => (y) => x + y;
const multiply = (x) => (y) => x * y;
const subtract = (x) => (y) => y - x;

const transform = pipe(
add(5),
multiply(3),
subtract(2)
);

console.log(transform(4));
console.log(transform(0));
  • 🔥 7
  • 👍 3
  • ❤ 2
Post #2914 3.5K
  • 🔥 4
  • ❤ 3
  • 👍 3
Post #2913 3.43K
CHALLENGE

class Counter {
#value = 0;

get count() {
console.log('Getting count');
return this.#value;
}

set count(val) {
console.log('Setting count to', val);
this.#value = Math.max(0, val);
}
}

const counter = new Counter();
counter.count = -5;
console.log(counter.count);
  • ❤ 4
  • 👍 4
  • 🔥 2
Post #2912 3.19K
  • ❤ 5
  • 👍 5
Post #2911 3.35K
CHALLENGE

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

const name = 'Sarah';
const score = 95;
const message = highlight`Hello ${name}, your score is ${score}!`;
console.log(message);
  • 🔥 5
  • 👍 2
Post #2910 3.71K
  • ❤ 5
  • 👍 3
  • 🔥 3
Post #2909 3.49K
CHALLENGE

async function processData() {
const promise1 = Promise.resolve('first');
const promise2 = Promise.reject('error');
const promise3 = Promise.resolve('third');

try {
const result = await Promise.allSettled([promise1, promise2, promise3]);
console.log(result[0].status);
console.log(result[1].reason);
console.log(result[2].value);
} catch (error) {
console.log('caught:', error);
}
}

processData();
  • ❤ 8
  • 🔥 2
Post #2908 3.5K
  • 🔥 6
  • ❤ 5
  • 👍 1
Post #2907 3.6K
CHALLENGE

const data = [
{ type: 'income', amount: 1000, category: 'salary' },
{ type: 'expense', amount: 200, category: 'food' },
{ type: 'income', amount: 500, category: 'freelance' },
{ type: 'expense', amount: 150, category: 'transport' }
];

const result = data.reduce((acc, item) => {
const key = item.type;
acc[key] = (acc[key] || 0) + item.amount;
return acc;
}, {});

console.log(result.income - result.expense);
  • 👍 4
  • ❤ 3
  • 🔥 1
Post #2906 3.59K
👀 For years, Mozilla, Apple, and the CSS Working Group have been working to bring "masonry" layouts (as above) natively to CSS. The concept is now called CSS Grid Lanes and here's how it works. You can already try it out in Safari Technology Preview 234.
  • ❤ 5
  • 👍 3
  • 🔥 3
Post #2905 3.8K
  • 👍 5
  • ❤ 3
  • 🔥 2
Post #2904 3.29K
CHALLENGE

console.log(typeof myVar);
console.log(typeof myFunc);
console.log(typeof myArrow);

var myVar = 'initialized';

function myFunc() {
return 'function declaration';
}

var myArrow = () => 'arrow function';

console.log(typeof myVar);
console.log(typeof myFunc);
console.log(typeof myArrow);
  • ❤ 3
  • 👍 2
  • 🔥 1
Post #2902 3.65K
  • 👍 5
  • 🔥 3
  • ❤ 2
Post #2901 3.62K
CHALLENGE

const config = { api: 'v1', timeout: 5000 };
Object.seal(config);

const settings = { theme: 'dark', lang: 'en' };
Object.freeze(settings);

config.api = 'v2';
config.retries = 3;
settings.theme = 'light';
settings.debug = true;

console.log(config.api);
console.log(config.retries);
console.log(settings.theme);
console.log(settings.debug);
  • 👍 4
  • 🔥 2
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 →