Let's assume that we are working with the following string:
javascriptlet sentence = 'My sister went to swim today.';
If you want to get the first two words:
javascriptlet firstTwoWords = sentence.split(' ').slice(0, 2).join(' '); // My sister
To get the first n words:
javascriptlet 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.