🕵️ Reverse EngineeringHow not to solve Jane Street's ASIC puzzle. Kinda.(17.09.2026 um 21:27 Uhr)
🔧 ProgrammierungHTMX is fine until the third stakeholder wants a modal(17.09.2026 um 21:13 Uhr)
🕵️ Reverse EngineeringHow not to solve Jane Street's ASIC puzzle. Kinda.(17.09.2026 um 21:27 Uhr)
🔧 ProgrammierungHTMX is fine until the third stakeholder wants a modal(17.09.2026 um 21:13 Uhr)
🔧 Programmierung 🕛 vor 3 Monaten 3 Min Lesezeit
0

Stupid Javascript Tricks

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Started a collection of elegant syntactical Javascript tricks that — fair warning — may not always be the most performant way to do what we're doing.






Joining an array of optional values



You have this object, and a bunch of properties that it might have...but how to gather them into a coherent list?



Traditionally, you might loop over a list of possible properties, checking for each one and accumulating them into a list manually:




CODE
const options = ['first', 'middle', 'last'];
const names = { first: 'John', last: 'Doe' };

let displayName = [];
for (const prop of options) {
if (names.hasOwn(prop)) {
displayName.push(names[prop]);
}
}
displayName = displayName.join(' ');






This works, but it involves maintaining that list of properties, looping through said list, and doing some manual clean up. A more elegant way is to use to discard any falsey values:




CODE
const names = { first: 'John', last: 'Doe' };
const displayName = [ names?.first, names?.middle, names?.last ]
.filter(Boolean)
.join(' ')
;
// -> 'John Doe'









Wrapping in an array



Say that you have a value that you want to iterate, but it may or may not already be an array.



The more explicit (and arguably more readable) approach would be to test the value beforehand with an explicit :




CODE
const wrapInArray = (maybeAnArray) => [ maybeAnArray ].flat();
// ^ much shorter ^

wrapInArray( 'a string' );
// -> [ 'a string' ]

wrapInArray( [ 'a', 'list', 'of', 'strings' ] );
// -> [ 'a', 'list', 'of', 'strings' ]






Note that .flat only collapses the outer most array — any already nested arrays are preserved:




CODE
wrapInArray( [ ['several'], ['nested'], ['lists'] ] );
// -> [ [ 'several' ], [ 'nested' ], [ 'lists' ] ]









Deep destructuring



Sometimes you don't need the entire return value from something, just an object property here or an array item there.



Destructuring just what you need can make for clean and elegant code.




CODE
// connect to a database and query for something
import { Client } from 'pg'
const client = await new Client().connect();

// get only the rows and ignore the rest of the result
const { rows } = await client.query('SELECT "id", "name", "email" FROM "Users"');
// rows -> [ {id, name, email}, {...}, ... ]






But what if you need deeply nested objects and/or arrays? You might be surprised how deep you can go.




CODE
// get only the first row and ignore the rest
const { rows: [ firstRow ] = await client.query('SELECT "id", "name" FROM "Products"');
// firstRow -> {id, name}

// get first _property_ of first row
const { rows: [ { count } ] } = await client.query('SELECT COUNT(*) FROM "Products"');
// count -> '99'

const { rows: [ { listCol: [ , secondItem ] } ] } = await client.query(`SELECT json_build_array('foo', 'bar', 'baz') AS "listCol"`);
// secondItem -> 'bar'









Destructuring to existing variables



You'll typically declare a new variable as you destructure into it, but you can actually use an already declared variable!



The trick is wrapping the entire assignment in parentheses, to group it into an expression the compiler recognizes.




CODE
let valueInOuterScope;
try {
// without a declaration (const, let), throws a SyntaxError
{ result: valueInOuterScope } = funcThatCouldFail();
// 👍
( { result: valueInOuterScope } = funcThatCouldFail() );
} catch (err) {
console.error('something went wrong:', err);
}


Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
China's FamousSparrow APT Spies on US Politics in Latin America
1 Quelle
Researchers find way to listen in on headphones from afar
1 Quelle
Roku rolls out over 30 subscription bundles for up to 30% off, plus a new Labs feature
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Stupid Javascript Tricks

Thematisch verwandte Begriffe: Stupid, Javascript, Tricks · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...