Get the first two words of a string

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

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

If you want to get the first two words:

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

// My sister

To get the first n words:

javascript
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.