Get first word of string

To get first word of string you can do this:

let myStr = "Hello World"
let firstWord = myStr.split(" ")[0]

split(" ") will convert your string into an array of words (substrings resulted from the division of the string using space as divider) and then you can get the first word accessing the first array element with [0].

See more about the split method.

Use regular expression

var totalWords = "foo love bar very much.";

var firstWord = totalWords.replace(/ .*/,'');

<script src=""></script>

Split again by a whitespace:

var firstWords = [];
for (var i=0;i<codelines.length;i++)
  var words = codelines[i].split(" ");

Or use String.prototype.substr() (probably faster):

var firstWords = [];
for (var i=0;i<codelines.length;i++)
  var codeLine = codelines[i];
  var firstWord = codeLine.substr(0, codeLine.indexOf(" "));