PHP implode() Equivalent in JavaScript

JavaScript's Array.prototype.join() mirrors PHP's implode() closely, including automatic stringification of non-string elements, but its default separator is a comma rather than an empty string.

Close equivalent Covers the common case, but has documented behavioral differences.

PHP source
implode()
Closest JavaScript API
Array.prototype.join()

Main limitation: JavaScript's `.join()` called with no arguments defaults to a comma (`,`) separator; PHP's `implode($array)` with no glue defaults to an empty string — a silent, easy-to-miss default mismatch.

How to use each function

Before comparing the two, here's how implode() and Array.prototype.join() work on their own.

PHP: implode()

implode(string $separator, array $array): string

Joins the elements of an array into a single string, placing $separator between each element. Non-string elements (integers, floats, booleans) are automatically converted to strings. The $separator may be omitted, defaulting to an empty string.

Returns: The concatenated string.

PHP
implode(", ", ["red", "green", "blue"]);
// "red, green, blue"

JavaScript: Array.prototype.join()

arr.join(separator = ',')

Joins array elements into a string, automatically stringifying numbers, booleans, and null (as an empty string) along the way — closer to PHP's implode() than Python's join(). The default separator is a comma if you call .join() with no argument, unlike PHP's empty-string default.

Returns: The joined string.

JavaScript
["red", "green", "blue"].join(", ");
// "red, green, blue"

Quick mapping

PHP implode()
Closest JavaScript API Array.prototype.join()

How it differs

  • JavaScript's .join() called with no arguments defaults to a comma (,) separator; PHP's implode($array) with no glue defaults to an empty string — a silent, easy-to-miss default mismatch.
  • Both languages automatically stringify non-string elements (numbers, booleans) when joining, unlike Python — this is one of the closer pairings in this list.
  • null and undefined elements are converted to empty strings by JavaScript's .join(); PHP converts null to an empty string too, so this edge case matches.

Examples

Basic join

PHP
implode(", ", ["a", "b", "c"]);
// "a, b, c"
JavaScript
["a", "b", "c"].join(", ");
// "a, b, c"
Same argument order as PHP's glue, just called as a method on the array instead of passed as the first function argument.

Default separator mismatch

PHP
implode(["a", "b", "c"]);
// "abc" (empty-string default)
JavaScript
["a", "b", "c"].join();
// "a,b,c" (comma default)
PHP's no-glue default is an empty string; JavaScript's no-argument default is a comma — always pass an explicit separator to avoid this mismatch.

Edge cases

Edge case

Always pass an explicit separator to JavaScript's .join() when porting PHP code — relying on the default comma when the PHP source used implode($array) (empty-string default) is a common, silent bug.

References

  • PHP Manual — implode — checked 2026-07-29
  • MDN Web Docs — Array.prototype.join() — checked 2026-07-29

Last verified 2026-07-29.