Search code examples
javascriptmethodsfluentchaining

Chaining array and string methods in javascript


I try to chain some array and string methods but it doesn't work. It'd be great if anyone could explain to me why a function such as this doesn't work:

const scream = text => text.split('').push('!').join('').toUpperCase()

Solution

  • You could use Array#concat to return an array with another value instead of Array#push, which returns the new length, but is not part of a fluent interface for a later joining (which needs an array).

    const scream = text => text.split('').concat('!').join('').toUpperCase();
    
    console.log(scream('hi'));