#16: `${[1,2,3]}` and `${{}}`
Topic: strings · Difficulty: ★★
console.log(`${[1,2,3]}`);
console.log(`${{}}`);Output
1,2,3 [object Object]
Why
Template literal interpolation always converts its expression to a string, using the same ToString abstract operation as String(value). Arrays define a custom toString that joins their elements with commas and no brackets, so [1,2,3] becomes '1,2,3'. Plain objects don't override toString, so they fall back to the default Object.prototype.toString, which produces the generic tag '[object Object]' — the same string you'd get from String({}) or {} + ''.
More strings puzzles