JavaScript
30.9K subscribers
1.23K photos
10 videos
33 files
904 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

class StringBuilder {
#parts = [];
append(s) {
this.#parts.push(s);
return this;
}
toString() {
return this.#parts.join('');
}
}

const sb = new StringBuilder().append('a').append('b').append('c');
console.log(`${sb}-${sb.append('d')}`);
❤1👍1
What is the output?
Anonymous Quiz
35%
abc-abcd
30%
abc-abc
29%
abcd-abcd
6%
abc-d
❤2🔥2🤔2👍1
CHALLENGE

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

function multiply(a, b, c = 1, ...rest) {
return a * b * c * (rest.reduce((s, n) => s + n, 0) || 1);
}

const curried = curry(multiply);
console.log(curried(2)(3), multiply.length);
👍5❤2🔥1🤔1
What is the output?
Anonymous Quiz
22%
NaN 2
42%
6 3
30%
6 2
6%
7 2
❤1👍1
CHALLENGE

const target = { a: 1, b: 2 };
const handler = {
get(obj, prop, receiver) {
if (prop in obj) {
return Reflect.get(obj, prop, receiver);
}
return `missing:${String(prop)}`;
},
set(obj, prop, value, receiver) {
if (typeof value === 'number') {
return Reflect.set(obj, prop, value * 2, receiver);
}
return Reflect.set(obj, prop, value, receiver);
}
};
const p = new Proxy(target, handler);
p.c = 5;
p.d = 'hello';
console.log(p.a, p.c, p.d, p.z);
❤3👍1🤔1
CHALLENGE

const sym = Symbol('id');
const target = { a: 1, [sym]: 2 };
Reflect.defineProperty(target, 'b', { value: 3, enumerable: false });

const handler = {
get(obj, prop, receiver) {
if (prop === 'a') return Reflect.get(obj, prop, receiver) * 10;
return Reflect.get(obj, prop, receiver);
}
};

const proxy = new Proxy(target, handler);
console.log(proxy.a, proxy.b, Reflect.ownKeys(target).length, Object.keys(target).length);
👍2🔥1
What is the output?
Anonymous Quiz
16%
1 3 3 1
34%
10 3 3 2
40%
10 3 3 1
9%
10 3 2 1
CHALLENGE

let a = 0;
let b = null;
let c = '';
let d;

a ||= 10;
b ??= 20;
c &&= 'changed';
d ??= a + b;

console.log(a, b, c, d);
❤2👍2🔥1
CHALLENGE

function build(a, b = a + 1, c = (() => a + b)()) {
a = 100;
return [a, b, c];
}
console.log(build(1), build(1, 2), build(5, 5, 5));
CHALLENGE

let outerType;
{
function greet() {
return 'inner';
}
outerType = typeof greet;
}
console.log(outerType, typeof greet);
❤1
CHALLENGE

const x = 2n ** 10n;
const y = 7n / 2n;
const z = -7n / 2n;
const w = BigInt(3.0) === 3n;
console.log(`${x} ${y} ${z} ${typeof x} ${w}`);
❤5
CHALLENGE

const obj = { a: { b: null, getC: () => 42 } };
let calls = 0;
function sideEffect() {
calls++;
return 10;
}
const result = obj.a?.b?.getC?.() ?? sideEffect();
const result2 = obj.a?.z?.getC?.() ?? sideEffect();
console.log(result, result2, calls);
👍2❤1🔥1
🤔2❤1
CHALLENGE

const counter = {
count: 0,
*[Symbol.iterator]() {
yield this.count++;
yield this.count++;
yield this.count++;
}
};

console.log([...counter], counter.count);

export {};
❤1