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

1 Answer

0 votes
function get_first_two_words(s) {
    var arr = s.split(" ");
     
    return arr[0] + " " + arr[1];
}
 
var s = "javascript php c c++ python"; 
 
document.write(get_first_two_words(s));
 
 
 
/*
run:
 
javascript php 
 
*/

 



answered Sep 7, 2019 by avibootz
...