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

1 Answer

0 votes
const s = "TypeScript is a programming language superset of JavaScript"; 
 
const N = 3;
 
const first3 = s.split(' ').slice(0, N).join(' ');
 
console.log(first3); 
 
 
 
 
/*
run:
 
"TypeScript is a" 
 
*/

 



answered Feb 7, 2022 by avibootz
...