How to get the first N words from a string in JavaScript

1 Answer

0 votes
const s = 'javascript c c++ java php';

const N = 3;

const first3 = s.split(' ').slice(0, N).join(' ');

console.log(first3); 




/*
run:

"javascript c c++"

*/

 



answered Feb 7, 2022 by avibootz
...