whatstheoutput_

#43: 'abc'.split('') vs 'abc'.split()

Topic: strings · Difficulty: ★

console.log('abc'.split(''));
console.log('abc'.split());

Output

[ 'a', 'b', 'c' ]
[ 'abc' ]

Why

Passing an empty string '' as the separator tells split() to break the string apart between every single character, yielding one array entry per character: ['a', 'b', 'c']. Calling split() with NO argument at all is a completely different case — per the spec, when the separator is undefined, split does not split anything and returns an array containing the whole original string as its only element, ['abc']. The empty-string separator and 'no separator at all' look similar but produce opposite results, which is a frequent source of bugs when a separator variable ends up undefined.

More strings puzzles

Play today's puzzle →