Today I Learned

JavaScript String.prototype.split() with "limit" parameter

July 10, 2021

Definition

A non-negative integer specifying a limit on the number of substrings to be included in the array. If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

Syntax

split(separator)
split(separator, limit)

Example

const str = "today is a good day right?";

str.split(" "); // ["today", "is", "a", "good", "day", "right?"]
str.split(" ", 4); // ["today", "is", "a", "good"]

Reference

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

← All posts