#57: split with a limit argument
Topic: strings · Difficulty: ★
console.log('a,b,c,d'.split(',', 2));Output
[ 'a', 'b' ]
Why
split() accepts an optional second argument, limit, that caps how many pieces the result array may contain. Splitting 'a,b,c,d' on commas would normally produce four pieces, ['a','b','c','d'], but with a limit of 2 the method stops collecting after the second piece and simply discards everything after that point — it does not keep splitting and truncate afterward, it just stops early. The result is ['a', 'b'], with 'c' and 'd' silently lost rather than joined back onto 'b'. This limit is easy to forget about and can quietly drop data when reused from code that assumed no limit.