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

1 Answer

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

 



answered Sep 7, 2019 by avibootz

Related questions

1 answer 166 views
1 answer 129 views
1 answer 132 views
2 answers 256 views
2 answers 240 views
1 answer 158 views
1 answer 144 views
...