- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Or if you're using a sane language, use the builtin
joinfunction.Admin
Above all, this!
Addendum 2026-02-18 07:36: And, curiously, despite all its many insanities, JS is sufficiently sane in this particular respect. Well, unless I've missed something about
Array.prototype.join()Admin
I have seen a lot of code where they always append the separator comma, and after the loop they take a substring of the result with all characters except the last. What they usually forget is the case when the array is empty and the for-loop is skipped. There it always crashes badly when taking the substring.
Another way of doing it:
Admin
oh well, the newline did not work. And I can't correct my pevious post... Trying with two slashes instead of backslashes ... Another way of doing it: // let arrayString = '{'; // let separator = "";// for(let i = 0; i < jsArray.length; i++) { // arrayString += separator + jsArray[i];// separator=",";// }// arrayString += '}';//
Admin
Yeah, that's how I do it.
Admin
''' use three single quotes the line breaks will work for you it's also monospaced
Addendum 2026-02-18 09:05: Well, that was wrong
Admin
Admin
You could also use a ternary:
But as @Dragnslcr said,
join()is the better way, and as @Steve_The_Cynic mentioned, JavaScript has it built-in:Admin
I'll also post the periodic reminder that the comments here now render Markdown, so you can use that for quotes, code, etc.
Admin
I don't even understand how that works.
I assume the second push is used to fill the
?in the first. However, doesn't that mean it is interpreted as a string? Not as an array? I mean: it's a string.And a broken one at that. How should Knex or Postgres know, that this array
["1", "2, 3", "4" ]is supposed to have three elements? Maybe not Little Bobby Tables broken, but broken nonetheless.Admin
My preference is to use an ORM for all the common, tedious cases (most inserts, updates and deletes fall in this category, as well as simple selects), and for the rest drop to plain old SQL with parameters that are automatically escaped. I've never understood the appeal of query generators - you just end up with a mess of a code which basically parallels an SQL query, but is much more verbose and hard to read. There might be some projects that benefit from SQL-dialect-agnosticism that a query builder provides, but 99% of all software out there makes the DB choice first and never switches from that. Plus when you start to use an SQL-dialect-agnostic code, you also LOSE access to all the specific features that might be available in your particular dialect. Like that @> operator.