How to get the first N words from a string in Node.js

1 Answer

0 votes
const s = 'node.js c c++ java php';

const N = 3;

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

console.log(first3); 





/*
run:

node.js c c++

*/

 



answered Feb 7, 2022 by avibootz
...