whatstheoutput_

#52: 'ab'.repeat(0)

Topic: strings · Difficulty: ★

console.log(JSON.stringify('ab'.repeat(0)));
console.log('ab'.repeat(3));

Output

""
ababab

Why

String.prototype.repeat(n) returns a new string containing n concatenated copies of the original. repeat(0) is a perfectly valid, well-defined call — zero copies of anything concatenated together is simply the empty string '', which is why it's wrapped in JSON.stringify here to make the otherwise-invisible empty string visible as "" instead of printing a blank line. repeat(3), by contrast, produces three back-to-back copies of 'ab', giving 'ababab'. Note repeat() throws a RangeError for negative counts, but 0 is explicitly allowed and simply yields an empty result.

More strings puzzles

Play today's puzzle →