PHP explode() Equivalent in JavaScript

JavaScript's String.prototype.split() plays the same role as PHP's explode(), but its limit argument truncates the result array rather than capping how many splits happen.

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

PHP source
explode()
Closest JavaScript API
String.prototype.split()

Main limitation: PHP's `explode($sep, $str, $limit)` positive limit controls how many *pieces* are produced, with all remaining text lumped into the final piece; JavaScript's `.split(sep, limit)` simply truncates the result array to `limit` items and silently discards the rest of the string — no remainder-lumping behavior at all.

How to use each function

Before comparing the two, here's how explode() and String.prototype.split() work on their own.

PHP: explode()

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

Splits a string into an array on a literal substring separator. A positive $limit caps the number of pieces returned, folding any remainder into the last element; a negative $limit removes that many elements from the end of the result.

Returns: An array of one or more string pieces.

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

JavaScript: String.prototype.split()

str.split(separator, limit)

Splits a string into an array on a literal string or regular-expression separator. Unlike PHP's explode(), the limit argument simply truncates the result array to that many items and discards the rest of the string, rather than folding the remainder into the last piece.

Returns: An array of string pieces.

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

Quick mapping

PHP explode()
Closest JavaScript API String.prototype.split()

How it differs

  • PHP's explode($sep, $str, $limit) positive limit controls how many *pieces* are produced, with all remaining text lumped into the final piece; JavaScript's .split(sep, limit) simply truncates the result array to limit items and silently discards the rest of the string — no remainder-lumping behavior at all.
  • JavaScript's .split() accepts a regular expression as the separator, unlike PHP's explode() which only accepts a literal string; for pattern-based splitting in PHP, compare to preg_split() instead.
  • JavaScript's .split("") (empty string separator) splits into individual characters, which is valid and common; PHP's explode("", $str) throws a ValueError for an empty separator — this is a real behavioral gap, not just cosmetic.

Examples

Basic split

PHP
explode(",", "a,b,c");
// ["a", "b", "c"]
JavaScript
"a,b,c".split(",");
// ["a", "b", "c"]
A direct, exact translation with no limit argument.

Limit argument (truncates, does not lump)

PHP
explode(",", "a,b,c,d", 2);
// ["a", "b,c,d"]
JavaScript
"a,b,c,d".split(",", 2);
// ["a", "b"]
PHP keeps the remaining text joined in the last element ("b,c,d"); JavaScript just throws away everything past the 2nd piece, losing "c" and "d" entirely — a real behavioral difference, not just cosmetic.

Edge cases

Edge case

JavaScript's .split(sep, limit) truncation means data loss if you assume PHP's "everything after the limit stays together" behavior — always double check whether you need .split(sep) with no limit and then slice/join manually to replicate PHP's lumping behavior.

References

  • PHP Manual — explode — checked 2026-07-29
  • MDN Web Docs — String.prototype.split() — checked 2026-07-29

Last verified 2026-07-29.