JavaScript
31.6K subscribers
1.17K photos
10 videos
33 files
844 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
25%
10
41%
7
21%
12
13%
15
❀2πŸ”₯2
CHALLENGE


"use strict";

function createCounter() {
let count = 0;

return {
increment() { count++; },
getCount() { return count; },
reset: function() { count = 0; }
};
}

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

const { getCount, reset } = counter;

try {
reset();
console.log("After reset:", counter.getCount());
} catch (e) {
console.log("Error:", e.message);
}

console.log("Direct call:", counter.getCount());
❀5πŸ‘1πŸ”₯1
CHALLENGE


const obj = {
name: "Quantum",
regular: function () {
return this.name;
},
arrow: () => {
return this?.name;
},
nested: function () {
const inner = () => this.name;
return inner();
},
};

console.log(obj.regular());
console.log(obj.arrow());
console.log(obj.nested());
❀3πŸ‘2πŸ”₯1
❀5πŸ‘3πŸ”₯2πŸ€”1
CHALLENGE

const curry = (fn) => {
const arity = fn.length;
return function curried(...args) {
if (args.length >= arity) {
return fn(...args);
}
return (...moreArgs) => curried(...args, ...moreArgs);
};
};

const volume = (l, w, h) => l * w * h;
const curriedVolume = curry(volume);

const withLength5 = curriedVolume(5);
const withLength5Width3 = withLength5(3);

console.log(typeof withLength5);
console.log(typeof withLength5Width3);
console.log(withLength5Width3(4));
console.log(curriedVolume(2)(6)(7));
❀5πŸ‘2πŸ”₯2
❀5πŸ‘2πŸ”₯2πŸ€”1🀣1
CHALLENGE

function highlight(strings, ...values) {
return strings.reduce((result, str, i) => {
const value = values[i - 1];
const formatted =
typeof value === "number"
? `[${value * 2}]`
: `<${String(value).toUpperCase()}>`;
return result + formatted + str;
});
}

const language = "javascript";
const year = 2015;
const feature = "templates";

const output = highlight`Language: ${language}, introduced in ${year}, feature: ${feature}!`;
console.log(output);
❀6πŸ‘2πŸ”₯2
CHALLENGE

const handler = {
get(target, prop, receiver) {
if (prop in target) {
return Reflect.get(target, prop, receiver) * 2;
}
return `Missing: ${prop}`;
},
set(target, prop, value) {
if (typeof value !== "number") {
throw new TypeError("Only numbers allowed");
}
return Reflect.set(target, prop, Math.abs(value));
},
};

const store = new Proxy({ gold: 10, silver: 5 }, handler);

store.bronze = -42;

console.log(store.gold);
console.log(store.bronze);
console.log(store.platinum);
❀4πŸ”₯3
❀4πŸ”₯2πŸ‘1
CHALLENGE

function* range(start, end) {
while (start < end) {
yield start++;
}
}

function* evens(iter) {
for (const val of iter) {
if (val % 2 === 0) yield val;
}
}

function* take(n, iter) {
let count = 0;
for (const val of iter) {
if (count++ >= n) return;
yield val;
}
}

function* pipeline() {
yield* take(3, evens(range(1, 20)));
yield* take(2, range(10, 15));
}

const result = [...pipeline()];
console.log(result);
πŸ”₯2πŸ‘1
πŸ€” axios Package Compromised; Malicious Versions Added a Trojan Dependency

Axios is an HTTP library that gets 100M+ downloads a week, largely due to its legacy popularity. An attacker took advantage of that to roll out a version with a malicious dependency including a remote access trojan (though Axios' codebase itself was fine). This is big, as even if you don’t use Axios, your dependencies might. Here's how to see if you're affected.

Ashish Kurmi
Please open Telegram to view this post
VIEW IN TELEGRAM
❀4πŸ”₯4πŸ€”3🀩1
CHALLENGE

const delay = (ms, val) => new Promise(res => setTimeout(res, ms, val));

const p1 = delay(300, "alpha");
const p2 = Promise.reject("network error");
const p3 = delay(100, "gamma");
const p4 = Promise.reject("timeout");

Promise.allSettled([p1, p2, p3, p4]).then(results => {
const summary = results.map(r =>
r.status === "fulfilled"
? `ok:${r.value}`
: `fail:${r.reason}`
);
console.log(summary.join(" | "));
});
πŸ‘3πŸ”₯2❀1
CHALLENGE


const str = " Hello, World! ";

const result = str
.trim()
.split(", ")
.map((word, i) => {
if (i % 2 === 0) return word.toUpperCase();
return word.toLowerCase().replace("!", "@");
})
.reverse()
.join(" | ");

console.log(result);
❀3
πŸ”₯8❀5πŸ‘1
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘5πŸ”₯2❀1
CHALLENGE

function Vehicle(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.speed = 0;
}

Vehicle.prototype.accelerate = function (amount) {
this.speed += amount;
return this;
};

Vehicle.prototype.describe = function () {
return `${this.year} ${this.make} ${this.model} going ${this.speed}km/h`;
};

function ElectricVehicle(make, model, year, range) {
Vehicle.call(this, make, model, year);
this.range = range;
}

ElectricVehicle.prototype = Object.create(Vehicle.prototype);
ElectricVehicle.prototype.constructor = ElectricVehicle;

ElectricVehicle.prototype.describe = function () {
return Vehicle.prototype.describe.call(this) + ` | Range: ${this.range}km`;
};

const car = new ElectricVehicle("Tesla", "Model 3", 2023, 500);
car.accelerate(60).accelerate(40);

console.log(car.describe());
console.log(car instanceof ElectricVehicle);
console.log(car instanceof Vehicle);
console.log(car.constructor === ElectricVehicle);
❀2