Get the first two words of a string

Let's assume that we are working with the following string:

let sentence = 'My sister went to swim today.';

If you want to get the first two words:

let firstTwoWords = sentence.split(' ').slice(0, 2).join(' ');

// My sister

To get the first n words:

let n = 5;

let firstNWords = sentence.split(' ').slice(0, n).join(' ');

// My sister went to swim

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.